MSFN Forum: How to move files to directories - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

How to move files to directories Rate Topic: -----

#1 User is offline   DosCode 

  • Newbie
  • Group: Members
  • Posts: 47
  • Joined: 15-February 12
  • OS:none specified
  • Country: Country Flag

Posted 16 February 2012 - 05:25 PM

I have just downloaded these files and would like to make directories to them. I will shorten the list of files coz it is really long.
EKAH-ADC_en.pdf
EKAH-APDC_en.pdf
EKAH_en.pdf
EKAH_PATC.pdf
EKBI-ADC_en.pdf
EKBI-APDC_en.pdf
EKBI-ILS-09-(CAT I_II_III)-(ACFT CAT A_B)_en.pdf
EKBI-SID-09-1_en.pdf
EKBI_en.pdf
EKCH-AOC-A 04L_en.pdf
EKCH-AOC-A 30_en.pdf
EKCH-APDC SOUTH_en.pdf
EKCH-GMC-4_en.pdf
EKCH-STANDARD TAXI ROUTES-22L_en.pdf
EKCH_ADC_en.pdf
EKCH_PATC_22L.pdf
EKCH_STANDARD-TAXI-ROUTES-04L_en.pdf
EKEB-ADC_en.pdf
EKEB-NDB 26 (ACFT CAT B)_en.pdf
EKEB_en.pdf
EKEB_ILS_DME_26_ES_A_en.pdf
EKEB_PATC_26.pdf
EKHG-ADC_en.pdf
EKHG-SRE-27_en.pdf
EKHG_en.pdf
EKKA-ADC_en.pdf
EKYT ADC_en.pdf
EKYT ATC-26R_en.pdf
EKYT DME 08L_en.pdf
EKYT en.pdf



But the name could be also for example:
_EKAH-ADC_en.pdf

The new directory should be called from the 4 characters at begin of filename.
So EKAH-ADC_en.pdf needs EKAH directory and move it there.

Could you please help me to do this script?


#2 User is offline   5eraph 

  • Update Packrat
  • Group: Supreme Sponsor
  • Posts: 954
  • Joined: 04-July 05
  • OS:XP Pro x64
  • Country: Country Flag

Posted 16 February 2012 - 06:10 PM

The following should do what you ask. Line 5 removes a leading underscore from the potential directory name if it exists. Line 6 checks for the existence of the needed directory and creates it if it does not exist. Line 7 does the actual moving.

"ECHO=" should be removed from both lines 6 and 7 when you're satisfied it works as intended.

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "delims=" %%G IN ('DIR /B /A-D "*.pdf" 2^>NUL') DO (
  SET "sPDFName=%%~nxG"
  IF "!sPDFName:~0,1!"=="_" (SET "sPDFName=!sPDFName:~1!")
  FOR /F "delims=" %%H IN ('DIR /B /AD "!sPDFName:~0,4!" 2^>^&1 1^>NUL') DO (ECHO=MD "!sPDFName:~0,4!")
  ECHO=MOVE "%%~nxG" "!sPDFName:~0,4!\"
)
PAUSE

This post has been edited by 5eraph: 16 February 2012 - 06:27 PM


#3 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,352
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 16 February 2012 - 06:34 PM

Here try this VBS script, just place it in the folder that you want the files to be rename.
RenameToFourCharacters.vbs
'-> Object For Runtime
 Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> Varibles For Runtime
 Dim Obj, V1, V2
'-> Loop Threw The Parent Folder File
  For Each Obj In Fso.GetFolder(".").Files 
'-> Separate The Script From Other Files
   If Not LCase(Right(Obj.Name,3)) = "vbs" Then
'-> Get The First Four Characters For The Shorten Name
    V1 = Left(Obj.Name,4)
'-> Get The Last Four Characters For The Extension
    V2 = Right(Obj.Name,4)
'-> Move The Old Name To The New Names
    Fso.MoveFile Obj.Path, Replace(Obj.Path,Obj.Name, V1 & V2)
   End If 
  Next 



Rename this RenameToFourCharacters.vbs.txt to RenameToFourCharacters.vbs to make active
[attachment=33964:RenameToFourCharacters.vbs.txt]

This does the same as above, but it produces a cmd promt window and displays old new name changes.
RenameFourChar.vbs
'-> Object For Runtime
 Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> Varibles For Runtime
 Dim Obj, V1, Arw: Arw = Chr(187) & Chr(160)
'-> Make Sure It Cscript.exe
  If InStr(1,WScript.FullName,"cscript",1) Then
    Rename()
  Else
   MsgBox vbTab & "Error " & Arw & "Wrong Scripting Engine" & vbCrLf & _
   Arw & "This script was ment to be run using Cscript.exe, and not" & vbCrLf & _
   "this scripting Wscript.exe. Right Click this script and select" & vbCrLf & _
   "either Cmd Promt or Cscript.exe from the menu",4128,"Error"
  End If
   Function Rename()
'-> Loop Threw The Parent Folder File
    For Each Obj In Fso.GetFolder(".").Files 
'-> Separate The Script From Other Files
     If Not LCase(Right(Obj.Name,3)) = "vbs" Then
'-> New File Name
      V1 = Left(Obj.Name,4) & Right(Obj.Name,4)
'-> Move The Old Name To The New Names If Not Exists
      If Not Fso.FileExists(Replace(Obj.Path,Obj.Name, V1)) Then
       WScript.StdOut.WriteLine "Old Name " & Arw & Obj.Name
       WScript.StdOut.WriteLine "New Name " & Arw & V1 & vbCrLf 
       WScript.Sleep 55      
       Fso.MoveFile Obj.Path, Replace(Obj.Path,Obj.Name, V1)
      Else
       WScript.StdOut.WriteLine "UnChange " & Arw & Obj.Name & vbCrLf
      End If
     End If 
    Next 
'-> Wait For The Enter Key To Be Pressed
    WScript.StdOut.WriteLine "Press Enter To Close Window"
    Do While WScript.StdIn.AtEndOfLine
    WScript.Quit()
    Loop
   End Function



#4 User is offline   DosCode 

  • Newbie
  • Group: Members
  • Posts: 47
  • Joined: 15-February 12
  • OS:none specified
  • Country: Country Flag

Posted 17 February 2012 - 04:33 AM

Thank you and I start with 5eraph's code. This is output.

MOVE "EKYT DME 08L_en.pdf" "EKYT\"
MD "EKYT"
MOVE "EKYT en.pdf" "EKYT\"
MD "EKYT"
MOVE "EKYT ILS_DME 08L_en.pdf" "EKYT\"
MD "EKYT"
MOVE "EKYT ILS_DME 26R (CAT I_II)_en.pdf" "EKYT\"
MD "EKYT"
MOVE "EKYT PDC_en.pdf" "EKYT\"
MD "EKYT"
MOVE "EKYT VOR_DME 26R_en.pdf" "EKYT\"
Press any key to continue...

It looks that everything should work, no error. But I can't find my folders.
Edit:
I think it is related to the command:
ECHO=MD "!sPDFName:~0,4!"


When I remove ECHO= so the command works. But no display of process. Thank you, you saved me a lot of work.

This post has been edited by DosCode: 17 February 2012 - 05:03 AM


#5 User is offline   DosCode 

  • Newbie
  • Group: Members
  • Posts: 47
  • Joined: 15-February 12
  • OS:none specified
  • Country: Country Flag

Posted 17 February 2012 - 08:00 AM

Can you explain me what does the command
DIR /B /AD "!sPDFName:~0,4!"
?
I see it first time with dir command - it looks like you used regular expression, but I am not able to reproduce it in command line separately. Even in batch when I write echo %sPDFName% so it prints some regular expression or what is it not, the value(?) (I thought it should keep value like four letter of the filename).

This post has been edited by DosCode: 17 February 2012 - 08:02 AM


#6 User is offline   5eraph 

  • Update Packrat
  • Group: Supreme Sponsor
  • Posts: 954
  • Joined: 04-July 05
  • OS:XP Pro x64
  • Country: Country Flag

Posted 17 February 2012 - 09:34 AM

DIR /B /AD "!sPDFName:~0,4!"

As written in the box above, this DIR command lists the directory ( /AD ) in bare format ( /B ) that is named after the first four characters ( :~0,4 ) of the variable sPDFName whose expansion is delayed ( !<VariableName>! ).

The variable sPDFName should contain a String value of the PDF file Name. To see what it should be we'll need to trace the code:

FOR /F "delims=" %%G IN ('DIR /B /A-D "*.pdf" 2^>NUL') DO (

For each file ending in ".pdf" that is not a directory, do the following.

SET "sPDFName=%%~nxG"

The name and extension ( nx ) of the current file ( %%G ) without surrounding quotes ( ~ ) is set to the sPDFName variable.

IF "!sPDFName:~0,1!"=="_" (SET "sPDFName=!sPDFName:~1!")

If the first character ( :~0,1 ) of the variable sPDFName is an underscore ( _ ) then set the value of the variable to itself ( sPDFName ) but skip the first character ( :~1 ). For example, if the file name is "_EKAH-ADC_en.pdf" then this line will set the variable equal to "EKAH-ADC_en.pdf".

To make it easier for me to explain, let's split the next line and explain the parts:

DIR /B /AD "!sPDFName:~0,4!"

If it exists, list the directory that is named for the first four characters ( :~0,4 ) of the variable sPDFName.

2^>^&1 1^>NUL

Let's make this easier to read by removing the escape characters ( ^ ) so we can see the output redirections at work:

2>&1 1>NUL

Send the error text output ( 2 ) as the new text output ( >&1). And ignore the original text output ( 1>NUL ).

Now let's combine the previous two segments:

DIR /B /AD "!sPDFName:~0,4!" 2>&1 1>NUL

If the directory named for the first four characters of the variable sPDFName does not exist then give us the error message.

Now let's try interpreting the entire FOR...DO expression:

FOR /F "delims=" %%H IN ('DIR /B /AD "!sPDFName:~0,4!" 2^>^&1 1^>NUL') DO (

If we receive an error message from the enclosed DIR command, do the following:

MD "!sPDFName:~0,4!"

Make a directory named after the first four characters ( :~0,4 ) of the variable sPDFName.

To summarize let's say the current file we're working on is "_EKAH-ADC_en.pdf":

  • First, sPDFName is set to the current file name ending in ".pdf": "_EKAH-ADC_en.pdf"

  • Next we skip the first character so the variable sPDFName is now "EKAH-ADC_en.pdf". This is the final change we make to the actual variable.

  • Finally, we use the following substring of the variable sPDFName several times. We skip zero characters and use the next four with the expression "!sPDFName:~0,4!". ("EKAH-ADC_en.pdf" becomes "EKAH", but the contents of the variable never change.)

EDIT: I suppose this line:

FOR /F "delims=" %%H IN ('DIR /B /AD "!sPDFName:~0,4!" 2^>^&1 1^>NUL') DO (ECHO=MD "!sPDFName:~0,4!")

...could be simplified to this:

DIR /B /AD "!sPDFName:~0,4!" 2>NUL || ECHO=MD "!sPDFName:~0,4!"


Here's the final code:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "delims=" %%G IN ('DIR /B /A-D "*.pdf" 2^>NUL') DO (
  SET "sPDFName=%%~nxG"
  IF "!sPDFName:~0,1!"=="_" (SET "sPDFName=!sPDFName:~1!")
  DIR /B /AD "!sPDFName:~0,4!" 2>NUL || MD "!sPDFName:~0,4!"
  MOVE "%%~nxG" "!sPDFName:~0,4!\"
)
PAUSE

The "ECHO=" have already been removed.

This post has been edited by 5eraph: 17 February 2012 - 10:05 AM


#7 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,364
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 17 February 2012 - 11:50 AM

Another solution - (no setting of variables or delayed expansion)
@ECHO OFF & SETLOCAL ENABLEEXTENSIONS
FOR %%# IN (*.PDF) DO (FOR /F "DELIMS=_- " %%$ IN ("%%#") DO (
		IF NOT EXIST "%%$\" MD "%%$"
		IF EXIST "%%$\" MOVE "%%~nx#" "%%$"))
PAUSE


#8 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,364
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 17 February 2012 - 03:02 PM

Topic Closed!

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy