Help with a Windows Cmd script


Thank you Denis, I will set this up. Is it okay for me to add the Unzip commands before and the move commands after your script that need to run? And if so, is there anything special that I need to do to the file to add them?

I screwed something up when cleaning it up before posting.
I'm just correcting the error now.
Back in half a mo.


Denis
 

My Computer

System One

  • OS
    Windows 11 Home x64 Version 23H2 Build 22631.3447
Here is the batch file that does what you want.
  • I have included a marker to show you where you can stick your unzipping commands.
  • I have included the move commands for you

You need to change the lines
Set "WorkingFolder=F:\Wherever\Whatever\TestRenaming"
Set "FinalFolder=F:\Wherever\Whatever\RenamingOutput"
  • for the WorkingFolder path you are going to extract the files to, and
  • for the FinalFolder path you are going to move them to afterwards.

You can call the batch file whatever you want. You just double-click on it to run it or create a shortcut to it & run that if it's more convenient.
RobL57Renaming.bat

:: Written for RobL57 in https://www.elevenforum.com/t/help-with-a-windows-cmd-script.16069/ Set /a Loop=1 Set "WorkingFolder=F:\Wherever\Whatever\TestRenaming" Set "FinalFolder=F:\Wherever\Whatever\RenamingOutput" ::::: This is where you would stick your unzipping commands ::::: You'll be able to make use of %WorkingFolder% for the unzipping destination For /F "tokens=*" %%V in (' Dir "%WorkingFolder%\FPH ???-?.mp3" /s /on /b ') do Call :ProcessFiles "%%V" GoTo EndProcessing :ProcessFiles :: Check the passed parameter, which will be the full path, name and ext of each file found :: I always wrap the passed parameter in "" if there is any possibility that the full path, name and ext of each file found might contain any spaces or special characters such as ampersands echo "%~1" :: Whilst I don't expect it to apply in your case, I always check for an empty variable being passed so that I can escape from that situation before trying to manipulate a blank variable If "FF%~1FF"=="FFFF" GoTo :EOF :: I often find it useful to set a new variable name so I can avoid further use of %~1 in processing. This does not make a difference in this case. Set "ThisVar=%~1" echo "%ThisVar%" :: The renaming and moving commands If %Loop% EQU 1 Ren "%ThisVar%" 5600.mp3 & Move /Y "%WorkingFolder%\5600.mp3" "%FinalFolder%\" If %Loop% EQU 2 Ren "%ThisVar%" 5601.mp3 & Move/Y "%WorkingFolder%\5601.mp3" "%FinalFolder%\" If %Loop% EQU 3 Ren "%ThisVar%" 5602.mp3 & Move/Y "%WorkingFolder%\5602.mp3" "%FinalFolder%\" If %Loop% EQU 4 Ren "%ThisVar%" 5603.mp3 & Move/Y "%WorkingFolder%\5603.mp3" "%FinalFolder%\" If %Loop% EQU 5 Ren "%ThisVar%" 5604.mp3 & Move/Y "%WorkingFolder%\5604.mp3" "%FinalFolder%\" If %Loop% EQU 6 Ren "%ThisVar%" 5605.mp3 & Move/Y "%WorkingFolder%\5605.mp3" "%FinalFolder%\" Set /a Loop=%Loop%+1 GoTo :EOF :EndProcessing Pause at EndProcessing during testing


I have tested it.
Before - showing WorkingFolder
Before.png
After - showing FinalFolder & WorkingFolder
After.png


I always stick scripts in a C:\Tools folder I've created & put special protection on so I can be fairly confident that I will not run a script that has been hacked behind my back.
Set up my Tools folder - TenForums



Denis
 

Attachments

  • RobL57Renaming.bat
    1.9 KB · Views: 3

My Computer

System One

  • OS
    Windows 11 Home x64 Version 23H2 Build 22631.3447
Denis,

I'm completely floored by your generosity here to assist me. I've been going crazy trying to figure this out on my own for two weeks now. Without your help I never would have been able to get it done right.
I will set this up in the morning, as it's 1am local time here.

Thank you so much for putting up with me while trying to explain this.
I will respond here after I set it up and try it out!
Rob
 

My Computer

System One

  • OS
    Windows 11 Pro
    Computer type
    PC/Desktop
    CPU
    12th Gen Intel(R) Core(TM) i7-12700 2.10 GHz
    Memory
    16Gb
Rob,

You'll notice that using DelayedExpansion, as garlin suggested in his sample script, achieves brevity.
My sub-routine structure is much longer but I find it easier to develop, to debug and [in the fullness of time] to maintain.

Is it okay for me to add the Unzip commands
Yes. I've shown you in the code where to add them.
::::: This is where you would stick your unzipping commands
::::: You'll be able to make use of %WorkingFolder% for the unzipping destination
You can just edit the script in Notepad to add them.

From what you wrote earlier [assuming it was not just an off-the-cuff example, I don't know 7z]
"C:\Program Files\7-Zip\7z" e D:\RockBlock\zipped\*.zip -oD:\RockBlock\unzipped\
your Working & Final folder lines would be
Set "WorkingFolder=D:\RockBlock\zipped" Set "FinalFolder=D:\RockBlock\unzipped"
and your unzipping command would be
"C:\Program Files\7-Zip\7z" e "%WorkingFolder%\*.zip" -o"%FinalFolder%\"
Is there really supposed to be a space after that lonely e? And no dash in front of it?

And you'd add the line
TimeOut /T 10
after that.
  • I assume that you need to wait 10 seconds to let the unzipping command complete.
  • It must be one of those commands that passes on through the script as soon as it is ordered rather than after it has completed.
You could experiment with the Start command so you can avoid the TimeOut /T 10 but there are some commands that cannot be made to wait even using that so it might only be worth bothering if you've nothing better to do.

the move commands after
I have included them within my script so you do not need to bother.



Denis
 
Last edited:

My Computer

System One

  • OS
    Windows 11 Home x64 Version 23H2 Build 22631.3447
Here's version 2 in which I have
1 Set the folder paths to what you posted earlier
2 Included your unzipping command [but do check it because I do not use 7zip]
3 Included your TimeOut command
4 Tidied the sub-routine up a bit - it just needs a single renaming & moving line now instead of six lines.

RobL157-Renaming-V2.bat
:: Written for RobL57 in https://www.elevenforum.com/t/help-with-a-windows-cmd-script.16069/ ::::: ::::: :: Initialisation Set /a Loop=1 Set "WorkingFolder=D:\RockBlock\zipped" Set "FinalFolder=D:\RockBlock\unzipped" ::::: ::::: ::::: This is where I have stuck your unzipping command ::::: I think this is what you want based on your earlier post but do check its syntax is correct since I do not use 7zip "C:\Program Files\7-Zip\7z" e "%WorkingFolder%\*.zip" -o"%FinalFolder%\" TimeOut /T 10 ::::: ::::: For /F "tokens=*" %%V in (' Dir "%WorkingFolder%\FPH ???-?.mp3" /s /on /b ') do Call :ProcessFiles "%%V" GoTo EndProcessing ::::: ::::: :ProcessFiles :: Check the passed parameter, which will be the full path, name and ext of each file found :: I always wrap the passed parameter in "" if there is any possibility that the full path, name and ext of each file found might contain any spaces or special characters such as ampersands echo "%~1" :: Whilst I don't expect it to apply in your case, I always check for an empty variable being passed so that I can escape from that situation before trying to manipulate a blank variable If "FF%~1FF"=="FFFF" GoTo :EOF :: I often find it useful to set a new variable name so I can avoid further use of %~1 in processing. This does not make any difference in this case. Set "ThisVar=%~1" echo "%ThisVar%" :: The renaming and moving commands Set /a FileNumber=5600-1+%Loop% Ren "%ThisVar%" "%FileNumber%.mp3" & Move /Y "%WorkingFolder%\%FileNumber%.mp3" "%FinalFolder%\" Set /a Loop=%Loop%+1 GoTo :EOF ::::: ::::: :EndProcessing Pause at EndProcessing during testing

I've added all those ::::: blank lines above just to improve legibility in this post. They are not in the batch file itself.


All the best,
Denis
 

Attachments

  • RobL157-Renaming-V2.bat
    1.6 KB · Views: 2
Last edited:

My Computer

System One

  • OS
    Windows 11 Home x64 Version 23H2 Build 22631.3447
I would have done the rename like this to archive the old files.. but it depends on his overall goal.. (testing it would need to be placed in the folder containing the files unless adjusted) But the provided answer was perfectly fine..

Batch:
CD /D "%~dp0"
REN 560*.mp3 560*-%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,2%.archive
SET step=5600
FOR %%i IN (*-*.mp3) DO (CALL :RENAMEACTION "%%i")
EXIT

:RENAMEACTION <originalfile>
REN %1 "%step%.mp3"
SET /a step+=1
EXIT /b

If you dont want to run a script like this from inside the folder containing the mp3's, instead of using:
Batch:
CD /D "%~dp0"
use
Batch:
CD /D "Path to the folder goes here"

The Timeout is unnecessary because CMD will execute the extraction completely before moving on to the next line, no need to wait pragmatically. The rename wildcards and the single FOR loop also move through the filenames in alphanumeric order, assuring the filenames will be ordered correctly.. (it is possible to reverse a for loop, but in this instance it moves in the correct direction)

You could even setup a short powershell monitor to watch a folder and when zip files are dropped into it, it can automatically execute the batch to extract/rename/move the following daily shows..

Powershell:
$host.UI.RawUI.WindowTitle = "ZipWatcher v1.0"
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS $true/$false
    $showzip = New-Object System.IO.FileSystemWatcher
    $showzip.Path = "C:\NewShows"
    $showzip.Filter = "*.zip"
    $showzip.IncludeSubdirectories = $false
    $showzip.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $renameandmove = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "ZipWatcherLog.txt" -value $logline
                Invoke-Item C:\Renamer\Renamer.cmd
              }
### DECIDE WHICH EVENTS SHOULD BE WATCHED
    Register-ObjectEvent $showzip "Created" -Action $renameandmove
    Register-ObjectEvent $showzip "Changed" -Action $renameandmove
    while ($true) {sleep 2}

If you went to those lengths you would probably want to have the batch move the original zip out of the folder as soon as the extract was done to avoid triggering the monitor by mistake or leaving old zips to be processed... ;-)
 
Last edited:

My Computer

System One

  • OS
    PE
Crap, i forgot about the time variable leaving a blank space in the beginning between midnight and 10am e.g. 00:00-09:59 the first zero shows as blank on the variable..

Needs to be dealt with to maintain the order of the archives

Batch:
CD /D "%~dp0"
SET timestamp=%time%
IF %timestamp:~0,1%==[] SET timestamp=0%timestamp%
REN 560*.mp3 560*-%date:~4,2%%date:~7,2%%date:~10,4%%timestamp:~0,2%%timestamp:~3,2%%timestamp:~6,2%.archive
SET step=5600
FOR %%i IN (*-*.mp3) DO (CALL :RENAMEACTION "%%i")
EXIT

:RENAMEACTION <originalfile>
REN %1 "%step%.mp3"
SET /a step+=1
EXIT /b
 
Last edited:

My Computer

System One

  • OS
    PE
Good Morning,

I just downloaded the v2.bat file and ran a test. The file was unzipped to the correct directory, but it didn't rename any of the files, they remain in the unzipped folder. I think I see why.
1688436746574.png
This line the working folder can't be the "Zipped" folder. In the unzipping portion of the script, they were moved to the "Unzipped" folder. So, I changed output of the unzipping process to stay in the Zipped folder, then the script found the files to be renamed. I changed the Final Folder to the location where those renamed files can be ingested. I tested this and it worked flawlessly.
For cleanup of the garbage that I need to delete, plus move the zip file to the archive folder, I will delete everything in the unzipped folder and move the original zip file to the archive directory for storing, and I believe this will be a completed task.
Thank you so much!!
 

My Computer

System One

  • OS
    Windows 11 Pro
    Computer type
    PC/Desktop
    CPU
    12th Gen Intel(R) Core(TM) i7-12700 2.10 GHz
    Memory
    16Gb
Oh dear. Yes, of course.
My brain was obviously not working when I added your unzipping commands to the batch file. That's the penalty for adding in lines I could not test myself.

I should have added a third variable:
where you want to start from [your zipped folder, which could be called SourceFolder],
where the work will be done [your unzipped folder, my WorkingFolder], and
where you want to move the renamed files to [my FinalFolder].

Do you want me to change this?
Can the cleaning up steps at the end be specified precisely?
[I wanted to combine the Loop & FileNumber variables anyway. There's no benefit in having them separate.]


Denis
 

My Computer

System One

  • OS
    Windows 11 Home x64 Version 23H2 Build 22631.3447
Hi Denis

If you think best, then change it. But once the files are renamed and moved to the Ingest folder on the C Drive, I don't know whether it matters.

The Clean up is only one clean up step:
Move D:\FPH\zipped\*.zip D:\FPH\archive\

That empties that Zipped folder so it's ready for next week.

Thank you
 

My Computer

System One

  • OS
    Windows 11 Pro
    Computer type
    PC/Desktop
    CPU
    12th Gen Intel(R) Core(TM) i7-12700 2.10 GHz
    Memory
    16Gb
Back
Top Bottom