Jump to content

How to move files to directories


Recommended Posts

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?

Link to comment
Share on other sites


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

Edited by 5eraph
Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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.

Edited by DosCode
Link to comment
Share on other sites

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).

Edited by DosCode
Link to comment
Share on other sites

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":

  1. First, sPDFName is set to the current file name ending in ".pdf": "_EKAH-ADC_en.pdf"
  2. 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.
  3. 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.

Edited by 5eraph
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...