DosCode Posted February 16, 2012 Share Posted February 16, 2012 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.pdfEKAH-APDC_en.pdfEKAH_en.pdfEKAH_PATC.pdfEKBI-ADC_en.pdfEKBI-APDC_en.pdfEKBI-ILS-09-(CAT I_II_III)-(ACFT CAT A_B)_en.pdfEKBI-SID-09-1_en.pdfEKBI_en.pdfEKCH-AOC-A 04L_en.pdfEKCH-AOC-A 30_en.pdfEKCH-APDC SOUTH_en.pdfEKCH-GMC-4_en.pdfEKCH-STANDARD TAXI ROUTES-22L_en.pdfEKCH_ADC_en.pdfEKCH_PATC_22L.pdfEKCH_STANDARD-TAXI-ROUTES-04L_en.pdfEKEB-ADC_en.pdfEKEB-NDB 26 (ACFT CAT B)_en.pdfEKEB_en.pdfEKEB_ILS_DME_26_ES_A_en.pdfEKEB_PATC_26.pdfEKHG-ADC_en.pdfEKHG-SRE-27_en.pdfEKHG_en.pdfEKKA-ADC_en.pdfEKYT ADC_en.pdfEKYT ATC-26R_en.pdfEKYT DME 08L_en.pdfEKYT en.pdfBut the name could be also for example:_EKAH-ADC_en.pdfThe 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 More sharing options...
Guest Posted February 17, 2012 Share Posted February 17, 2012 (edited) 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 OFFSETLOCAL EnableDelayedExpansionFOR /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 February 17, 2012 by 5eraph Link to comment Share on other sites More sharing options...
gunsmokingman Posted February 17, 2012 Share Posted February 17, 2012 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 activeThis 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 More sharing options...
DosCode Posted February 17, 2012 Author Share Posted February 17, 2012 (edited) 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 February 17, 2012 by DosCode Link to comment Share on other sites More sharing options...
DosCode Posted February 17, 2012 Author Share Posted February 17, 2012 (edited) 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 February 17, 2012 by DosCode Link to comment Share on other sites More sharing options...
Guest Posted February 17, 2012 Share Posted February 17, 2012 (edited) 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^>NULLet's make this easier to read by removing the escape characters ( ^ ) so we can see the output redirections at work:2>&1 1>NULSend 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>NULIf 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 OFFSETLOCAL EnableDelayedExpansionFOR /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!\")PAUSEThe "ECHO=" have already been removed. Edited February 17, 2012 by 5eraph Link to comment Share on other sites More sharing options...
Yzöwl Posted February 17, 2012 Share Posted February 17, 2012 Another solution - (no setting of variables or delayed expansion)@ECHO OFF & SETLOCAL ENABLEEXTENSIONSFOR %%# IN (*.PDF) DO (FOR /F "DELIMS=_- " %%$ IN ("%%#") DO ( IF NOT EXIST "%%$\" MD "%%$" IF EXIST "%%$\" MOVE "%%~nx#" "%%$"))PAUSE Link to comment Share on other sites More sharing options...
Yzöwl Posted February 17, 2012 Share Posted February 17, 2012 Topic Closed! Link to comment Share on other sites More sharing options...
Recommended Posts