Rename with bat file from an specific number - batch-file

I have a bat file that rename all of the png files to data_ files. It starts from data_1 and data_2 .... I want to start with data_140 and data_141...
How to do that?
:: Renaming files
for %%a in (*.png) do (
set /a count+=1
set "fname=%%~a"
setlocal enabledelayedexpansion
ren "!fname!" data_!count!.png
endlocal
)

I don't see the problem. SET /A count+=1 starts with count=0 as it's not defined yet. If you want your script to start at 140 just do set count=139 before you enter the loop:
:: Renaming files
set count = 139
for %%a in (*.png) do (
set /a count+=1
set "fname=%%~a"
setlocal enabledelayedexpansion
ren "!fname!" data_!count!.png
endlocal
)

Related

I'm trying to make a batch file that moves files by random

I already have a code, but it keeps selecting the first file on the list and it's getting irritating. I have no idea what to do.
#echo off
setlocal enabledelayedexpansion
CD c:\"destination"\somefolder
set n=0
for %%f in (*.*) do (
set /a n+=1
set "file[!n!]=%%f"
)
set /a rand=(n*%random% %%4) /4
move "!file[%rand%]!" C:\destination\somefolder
pause
rand with 30bit size(value from 0 to 2^30-1 modulo n)
#echo off
setlocal EnableDelayedExpansion
pushd c:\"source"\somefolder
set "n=0"
for /f "tokens=*" %%f in ('dir /b /a-d *.*') do (
rem number files from 0 and use full filename with spaceses
set "file[!n!]=%%~ff"
set /a "n+=1"
)
popd
if %n% leq 32768 ( set /a "rand=%random%%%n%"
) else set /a "rand=((%random%<<15)+%random%)%%n%"
move "!file[%rand%]!" C:\destination\somefolder
pause
endlocal

While splitting txt file through batch script Exclamations are ommitting

Iam new to batch script and here I tried to split my text file into chunks for each 1 million rows.
Chunk files are generated as I expected, but inside the output file content Iam missing the Exclamations ( ! ) and even it skipping the immediate column after Exclamation. Please help me to get data as it is in original file into chunks!
#ECHO OFF
setLocal DisableDelayedExpansion
set limit=1000000
set feed_name=test.txt
set file=%Tgt_Dir%\%feed_name%
set lineCounter=1
set filenameCounter=1
set name=
set extension=
for %%a in (%file%) do (
set "name=%%~na"
set "extension=%%~xa"
)
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%file%) do (
set splitFile=!name!%date:~12,2%%date:~4,2%%date:~7,2%!filenameCounter!!extension!
if !lineCounter! gtr !limit! (
set /a filenameCounter=!filenameCounter! + 1
set lineCounter=1
echo Created !splitFile!.
)
echo %%a>> %Tgt_Dir%\!splitFile!
set /a lineCounter=!lineCounter! + 1
)
endlocal
It is a Tab delimiter file.
ScreenShot
You need to toggle delayed expansion.
setlocal DisableDelayedExpansion
for /f "tokens=*" %%a in (%file%) do (
Set "line=%%a"
setlocal EnableDelayedExpansion
set splitFile=!name!%date:~12,2%%date:~4,2%%date:~7,2%!filenameCounter!!extension!
echo(!line!>> %Tgt_Dir%\!splitFile!
if !lineCounter! gtr !limit! (
ENDLOCAL
set /a filenameCounter+=1
set lineCounter=1
echo Created file
) ELSE ENDLOCAL
set /a lineCounter=lineCounter + 1
)

batch for loop increment value ENABLEDELAYEDEXPANSION

I'm trying to make a simple batch-file to make 7zip-archives from all the files in it's directory.
I want the 7zip-archives to get names like a01.7z, a02.7z, a03.7z...
Apparently incrementing a value in a batch-for-loops isn't easy.
The setlocal ENABLEDELAYEDEXPANSION solution doesn't work on my computer (windows 10, 64-bit)
Someone suggested putting the increment-code in a subroutine:
set /a counter=0
for %%i in (*.*) do (
call :pass2
goto :cont
:pass2
set /a counter=%counter%+1
goto :EOF
:cont
"c:\Program Files\7-Zip\7z.exe" a a%counter% "%%i"
)
This doesn't work because somehow DOS doesn't understand the final "%%i" anymore and just outputs "%i".
Please teach me how to make a for-loop in batch with a counter.
This is the simplest way to generate two-digits numbers, with a left zero:
#echo off
setlocal EnableDelayedExpansion
set /A counter=100
for %%i in (*.*) do (
set /A counter+=1
"c:\Program Files\7-Zip\7z.exe" a a!counter:~1! "%%i"
)
setlocal enabledelayedexpansion
set /a counter=0
for %%i in (*.*) do (
set /a counter=!counter!+1
#echo "c:\Program Files\7-Zip\7z.exe" a a!counter! "%%i"
)
This adds 1 file per zip.

Random File Picker Picks Same Value On Each Execution (batch)

I'm trying to launch a random file with this software via a script - the only problem is that this script always selects the same random number on every launch. It's always 41 for me... Any suggestions?
#echo on
setlocal EnableDelayedExpansion
cd C:\Users\User\Documents\Downloads\Nintendo
set n=0
for %%f in (*.*) do (
set /A n+=1
set "file[!n!]=%%f"
)
set /A "rand=%random% * 100 / 32768+1"
"C:\Users\User\Downloads\fceux-2.2.2-win32\fceux.exe" "!file[%rand%]!"
That works for me:
#echo off
setlocal enabledelayedexpansion
c:
cd \windows
for %%f in (*.*) do (
set /A n+=1
set "file[!n!]=%%f"
)
rem change random seed value
for /l %%i in (0,1,%time:~-2%) do set /A tmp=!random!
set /A rand=!random! %% n-1
echo "!file[%rand%]!"

Batch script for copying and renaming files

I need batch script to copy all files from one directory to another and rename them all to a default name (ex. NAME54.pdf) and continue counting from destination`s maximum number in name.
I wrote some script but it seems not working:
#echo on
D:
set count=0
for %%a in (scans1\*.*) do (
set /a count+=1
)
set count1=0
for %%b in (scans\*.*) do (
set /a count1+=1
)
for /l %%c in (1,1,%count1%) do (
set /a count+=1
copy D:\scans\*.* D:\scans\NAME%count%.pdf
)
pause
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET /a count=0
for %%c in (%sourcedir%\*.*) do (
CALL :select
ECHO copy "%%c" "%destdir%\NAME!count!.pdf"
)
GOTO :EOF
:select
SET /a count+=1
IF EXIST "%destdir%\NAME%count%.pdf" GOTO select
GOTO :eof
I've set up distinct source and destination directories. You would need to change these names to suit your circumstances.
I've chosen to simply ECHO the required copy command so that you can see the resultant commands. You'd need to change ECHO copy to copy to actually execute the commands.
If you append >nul to the copy command, the 1 file(s) copied response will be suppressed.

Resources