Create .BAT file to rename files in current directory with input - batch-file

I am trying to write a small .bat script (with limited programming knowledge) that will allow users to enter a value for current version number (e.g. 01) and new version number (e.g. 02) and then use those two numbers inside a rename command.
echo off
cls
TITLE Version Updater
echo Use this script to update file names. This works similar to find and replace in other programs.
echo[
echo Enter the current version number (01 for example)
echo off
set /p current="Current Version: "
echo[
echo Enter the new version number (01 becomes 02 etc.)
echo off
set /p new="New Version: "
echo[
echo You will be updating all occurences of "%current%" in your file names to "%new%", if this is correct then continue, if not close this window.
pause
cd %CD%
rename *%current%* *%new%*
pause
I think the code is almost right, I am just having troubles with making it reference the current directory that it is run from. I am using this script with people who have limited computer skills, and I am not the network administrator so I cannot really do anything too in depth.
If someone could please help me tweak this then that would be great.
Cheers,
Mat
Edit 1 - Comment response
For example, test_01.tab changing to test_02.tab. It basically will replace all _01 parts of the file name and change them to _02. In this case, current = 01 and new = 02. The filenames are all different though, so you may have test_01.bat, testing_01.bat to change, so just renaming the 01 to 02 is how I assumed it would work.
Edit 2 - Current code
This is thanks to Stephan below, I believe the only thing it needs to be working correct is for the file path to be removed from the second part of the REN command so that it just has the file and extension and it should work.
#pushd %~dp0
#Echo off
CLS
setlocal enabledelayedexpansion
set /p current="Current Version: "
set /p new="New Version: "
for /R %%a in (*_%current%*) do (
set "Name=%%~a"
ren "%%~a" "!Name:_%current%=_%new%!"
)
pause
Edit 3 - Final Code (works, yay!)
#pushd %~dp0
#echo off
cls
TITLE CEF File Version Updater
echo This script will update CEF file versions for TLS rejections (E.G. 4XXX_XX_1 to 4XXX_XX_2). Backup files prior to running this script as a safety measure.
echo[
echo Enter the current version number (no underscore)
echo off
set /p current="Current Version: "
echo[
echo Enter the new version number (no underscore)
echo off
set /p new="New Version: "
echo[
echo You will be updating all occurences of "_%current%" in your file names to "_%new%", is this correct?
SET /P ANSWER=Do you want to continue (Y/N)?
if /i {%ANSWER%}=={y} (goto :yes)
if /i {%ANSWER%}=={yes} (goto :yes)
goto :no
:yes
rename *_%current%.* *_%new%.*
CLS
echo Renaming is complete, please check the new file names are correct.
pause
exit /b 0
:no
exit /b 1

#Echo off
setlocal enabledelayedexpansion
set /p current="Current Version: "
set /p new="New Version: "
for /R %%a in (*_%current%.*) do (
set "Name=%%~nxa"
ECHO ren "%%~a" "!Name:_%current%=_%new%!"
)
pause
(remove ECHO, if the output satisfies you)

This should do the trick.
#Echo off
set /p current="Current Version: "
set /p new="New Version: "
for /R "c:\your location" %%a in (*_%current%.*) do (
ren *_%current%.* *_%new%.*
)
pause
Below is before and after running the script on a couple of test-files.

Related

Can not sort tiff image to specific folder using batch script

I am currently trying to create a batch script so I can sort multiple tiff images in a folder by moving it to other specific folder according to user input. What I achieved so far is:
1. Can loop through all the files in the folder
2. Can open the image viewer to view the tiff file
3. Close off the image viewer program
4. Move the tiff file to specific folder
However, I do not know why my set /p is not registering user's input value, when I tried to echo the input out.
cmd gave me "ECHO is off" message.
Appreciate if anyone could give me a hand to resolve this problem.
Many thanks
G
#ECHO OFF
cd "C:\img\"
FOR /R %%f IN (*.tif) DO (
echo Current file is: %%f
start "" "C:\Program Files (x86)\Common Files\Global 360\Imaging\kodakprv.EXE" %%f
set /p name= Action:
IF "%name%" == "1" GOTO ONE
IF "%name%" == "2" GOTO TWO
IF "%name%" == "3" GOTO THREE
ECHO %name%
echo None of the above, BYE!
GOTO END
:ONE
echo "I pressed 1"
taskkill /IM kodakprv.EXE
move %%f "C:\img\1"
cls
:TWO
echo "I pressed 2"
taskkill /IM kodakprv.EXE
move %%f "C:\img\2"
cls
:THREE
echo "I pressed 3"
taskkill /IM kodakprv.EXE
move %%f "C:\img\3"
cls
)
:END
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
cd "C:\img\"
FOR /R %%f IN (*.tif) DO (
echo Current file is: %%f
start "" "C:\Program Files (x86)\Common Files\Global 360\Imaging\kodakprv.EXE" %%f
SET "name="
set /p name= Action:
for %%v in (1 2 3) do IF "!name!" == "%%v" (
set "validaction=Y"
echo "I pressed %%v"
taskkill /IM kodakprv.EXE
move %%f "C:\img\%%v"
pause
cls
)
if not defined validaction (
ECHO !name!
echo None of the above, BYE!
GOTO END
)
)
:END
A few little problems.
First, you can't use labels within a code block (parenthesised series of commands) and second the old favourite, delayedexpansion - if a variable is changed within a code block then unless delayed expansion is in effect, the original value will be used. If delayed expansion is in effect, then %var% refers to the original value and !var! to the current (run-time) value.
Note also that set /p does not change the value of the variable if user response is simply Enter - -hence the set "name=" above to clear its value. Of course, you can omit that line and the last-entered value of name will be used again if you just press Enter.
There are many articles on SO about delayed expansion - just use the search feature.

How to use a batch file to loop through several executables files to compress them (.7z) and rename the file to avoid ".exe.7z"

I'm trying to zip up 27 exe files in one go using a batch file. The purpose of this file is to create .7z files for a modified HBCD menu. This file is located in, ...\Documents\CD\HBCD\Programs the exe files are in ...\Documents\CD\HBCD\Programs\Files\SysinternalsSuite
I keep getting:
Error:
Incorrect command line
I keep searching around and trying different things but I can't get it to work.
File Name: 7zBatch.bat
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
pushd "%~dp0"
echo "-------------------------------------------------"
echo "||======== BATCH ZIPPER (.7z) for HBCD ========||"
echo "-------------------------------------------------"
:AskSource
echo Enter the location of the exe file(s)
set SOURCE=
set /p SOURCE=">>" %=%
echo "%SOURCE%", is that correct & goto ConfirmSource
:ConfirmSource
echo (Y)es or (N)o?
set INPUT=
set /p INPUT=">>" %=%
if /I "%INPUT%"=="y" goto AskDest
if /I "%INPUT%"=="n" goto AskSource
echo Incorrect input, is the location correct? & goto ConfirmSource
:AskDest
cd %SOURCE%
echo Enter the location of the zip file(s)
set DEST=
set /p DEST=">>" %=%
echo "%DEST%", is that correct & goto ConfirmDest
:ConfirmDest
echo (Y)es or (N)o?
set INPUT=
set /p INPUT=">>" %=%
if /I "%INPUT%"=="y" goto Execute
if /I "%INPUT%"=="n" goto AskDest
echo Incorrect input, is the location correct? & goto ConfirmDest
:Execute
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO (
set fname=%%G
set nfname=!fname:.exe=!
echo.
echo Found: !fname!
..\..\7z.exe a -t7z !nfname!.7z "!fname!" -mx5 -sdel -oC:%DEST%
timeout /t 10 /nobreak
)
ENDLOCAL
echo.
pause
Once it gets to the 7 Zip line it gives the error. I realized that the nfname variable was empty so I tried:
set nfname=!%%G:.exe=!
That didn't work so I tried deleting that and going with:
..\..\7z.exe a -t7z !fname:.exe=!.7z "!fname!" -mx5 -sdel -oC:%DEST%
That didn't work so I tried putting different things in quotes one by one and then everything:
"..\..\7z.exe" a -t7z "!fname:.exe=!.7z" "!fname!" -mx5 -sdel -oC:"%DEST%"
Still nothing. What am I missing?
#echo off
SETLOCAL
echo "-------------------------------------------------"
echo "||======== BATCH ZIPPER (.7z) for HBCD ========||"
echo "-------------------------------------------------"
:AskSource
echo Enter the location of the exe file(s)
set "SOURCE="
set /p "SOURCE=>>"
echo "%SOURCE%", is that correct
:ConfirmSource
echo (Y)es or (N)o?
set "INPUT="
set /p "INPUT=>>"
if /I "%INPUT%"=="y" goto AskDest
if /I "%INPUT%"=="n" goto AskSource
echo Incorrect input, is the location correct?
goto ConfirmSource
:AskDest
echo Enter the location of the zip file(s)
set "DEST="
set /p "DEST=>>"
echo "%DEST%", is that correct
:ConfirmDest
echo (Y)es or (N)o?
set "INPUT="
set /p "INPUT=>>"
if /I "%INPUT%"=="y" goto Execute
if /I "%INPUT%"=="n" goto AskDest
echo Incorrect input, is the location correct?
goto ConfirmDest
:Execute
FOR %%G IN ("%SOURCE%\*.exe") DO (
"%~dp0..\..\7z.exe" a -t7z "%DEST%\%%~nG.7z" "%%~fG" -mx5 -sdel
)
ENDLOCAL
echo.
pause
The pushd and cd removed. Unsure why 7z.exe would be
located 2 directories up from where the input executable
directory is located. 7z.exe is now located 2 directories
up from the script directory.
The -o switch for 7z.exe is only for extraction as
mentioned in the help file so is omitted.
Double quoted set of variables to avoid any trailing spaces.
The for loop changed to simple type as it gives the file
paths required without need of using dir.
EnableDelayedExpansion removed as the for variables
uses modifiers to get fullpath, name, drive etc. See
for /? about modifiers available.
Removed timeout as it complains if stdin is used and see
no reason to use it for this task.
Removed obsolete goto commands that go to the next line
with a command.

Batch File System cannot find file after Loop

I am creating a batch file to open remote Computer Management console by taking User ID as input and computer name from 2nd column from file data.csv. it works fine on first attempt. When it goes back to :start label. and ask for other input. it gives error. System cannot find file ./data.csv
My code is
:start
set /p Input="Enter User-ID:"
for /f "usebackq tokens=1-4 delims=," %%a in (".\data.csv") do (
if %input% ==%%a ("cmd /c Start /B /wait compmgmt.msc –a /computer=%%b")
)
cls
GOTO start
Good practice to use %~dp0 for paths in batch files (instead of relative paths like .) that way if the current working folder changes the file will always be located.
So change to %~dp0data.csv
:start
set /p Input="Enter User-ID:"
PUSHD
for /f "usebackq tokens=1-4 delims=," %%a in (".\data.csv") do (
if %input% ==%%a ("cmd /c Start /B /wait compmgmt.msc –a /computer=%%b")
)
POPD
cls
GOTO start
should restore sanity, pushing the directory then restoring it before the next cycle.

Batch File Loop Back to Start

I have scoured the site and have been able to extract code which virtually handles all that I am seeking to achieve. However, I cannot get the last bit to work which is to loop back to the start if I want to re-key another Sheet Number. The code I have:
#Echo Off
cd "C:\temp\CopiedTo"
del *.* /Q
CLS
:start
#Echo Off
Set /P SheetNo=Enter the Sheet Number to Copy:
if exist "C:\temp\%SheetNo%.txt" (Copy "c:\temp\%sheetNo%.txt" "c:\temp\copiedto"
) Else (Echo set choice=
set /p choice="This Sheet Number Is Currently Not in the Directory - Do you wish to Re-Key Again? Press 'y' and enter for Yes: "
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='y' goto start
)
You use the %choice% variable inside a block. So you need delayed expansion.
Put a
setlocal enabledelayedexpansion
at the beginning of your script and use !choice! instead of %choice% inside the block.
And: remove the echo before set choice= (probably there for testing)
Here is another way to do it, with a simpler interface.
Note that an automated del *.* on it's own is a dangerous command when the preceding cd command can fail and whatever directory is current can be nuked, so it's always best to specify the folder or do some extra testing to check if the CD failed.
The CD command now has the /d switch to cater for another drive being current.
#Echo Off
cd /d "C:\temp\CopiedTo"
del "C:\temp\CopiedTo\*.*" /Q
CLS
:start
#Echo Off
set "sheetno="
Set /P "SheetNo=Enter the Sheet Number to Copy (press enter to quit): "
if not defined sheetno goto :EOF
if exist "C:\temp\%SheetNo%.txt" (
echo copying "%sheetNo%.txt"
Copy "c:\temp\%sheetNo%.txt" "c:\temp copiedto" >nul
) Else (
echo "Sheet Number "%sheetNo%.txt" Is Currently Not in the Directory
)
goto :start

Append String to file name in dos Syntax Error Need

#ECHO off
title Rename Script
set dir1=%1
set STR=%2
set count=1
:Start
cls
echo 1. Rename Files
echo 2. Quit
set /p choice=I choose (1,2):
if %choice%==1 goto rename
if %choice%==2 exit
:rename
cls
echo Running Rename Script for STR=%STR%
FOR %%n in (%dir1% *.*) DO (
ren %%n %STR%%%n
echo %STR%%%n)
echo done
pause
C:>yogesh>LDK.bat C:\yogesh app
OUTPUT:
Running Rename Script for STR=app
The syntax of the command is incorrect.
appC:\yogesh
appa3dapi.dll
appHLTV-Readme.txt
apphltv.cfg
appkver.kp
applanguage.inf
appLDR.bat
appMp3dec.asi
appMss32.dll
appMssv12.asi
appMssv29.asi
appTrackerNET.dll
The batch file cannot be found.
C:\yogesh>
There are few issues with this script:
" The syntax of the command is incorrect." I do not know where is the problem in script.
How to get the count of number of files renamed ?
Files get renamed into the directory where the .bat file resides I want to rename the files in specified folder as argument in variable dir1
Please let me know if you need more information.
Here is a solution without using CD. You needed to put a \ between your dir1 variable and *.* instead of a space. You needed quotes to protect against spaces in names. Use FOR variable modifiers ~nx to get just the name and extension (removes any drive and path info). Finally, use SET /A to perform math.
#ECHO off
title Rename Script
set "dir1=%~1"
set "STR=%~2"
set count=1
:Start
cls
echo 1. Rename Files
echo 2. Quit
set /p choice=I choose (1,2):
if %choice%==1 goto rename
if %choice%==2 exit
:rename
cls
echo Running Rename Script for STR=%STR%
set cnt=0
FOR %%F in ("%dir1%\*.*") DO (
ren "%%F" "%STR%%%~nxF"
echo %STR%%%~nxF
set /a cnt+=1
)
echo %cnt% files were renamed.
echo done
pause
Here you go:
#ECHO off
title Rename Script
set /A count=1
:Start
cls
echo 1. Rename Files
echo 2. Quit
set /p choice=I choose (1,2):
if %choice%==1 goto rename
if %choice%==2 exit
:rename
cls
set /p STR=choose a start-string:
echo Running Rename Script for STR=%STR%
FOR %%n in (*.*) DO (
ren "%%n" "%STR%%%n"
echo "%STR%%%n"
set /A count+=1)
echo count %count%
echo done
pause
we can save the current path in OLDDIR and then change the path to the one the user gave, at the end of the script we'll go back to OLDDIR using CD
you need to set the counter with /A
UPDATE for dbenham:
A small proof :)) that it worked (for me) with filenames that contains spaces:

Resources