how to increment text filename using batch script - windows

With reference to increment folder name link, I want to ask how to change it from creating folder to creating text file?
Here's the edited version of the code:
#echo off
#For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do #(
Set Day=%%A
Set Month=%%B
Set Year=%%C
)
#echo off
setlocal enableDelayedExpansion
set "baseName=testing-%Year%%Month%%Day%-"
set "n=0"
for /f "delims=" %%F in (
'2^>nul dir /b /ad "%baseName%*."^|findstr /xri "%baseName%[0-9]*"'
) do (
set "name=%%F"
set "name=!name:*%baseName%=!"
if !name! gtr !n! set "n=!name!"
)
set /a n+=1
md "%baseName%%n%"
So it works like this. "Hello-20140716-1", "Hello-20140716-2" etc folder can be created by double clicking this batch file. But how can I change it from folder to text file?
I tried changing md "%baseName%%n%" to Echo "Hello" > %baseName%%n%.txt and Echo "Bye" > %baseName%%n%".txt
But this does not work as it will only output " Bye" into hello-20140716-1.txt without creating "Hello" to hello-20140716-1.txt and "Bye" to hello-20140716-2.txt

Test this:
#echo off
For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do #(
Set Day=%%A
Set Month=%%B
Set Year=%%C
)
set "baseName=testing-%Year%%Month%%Day%-"
set "n=0"
:loop
set /a n+=1
if exist "%baseName%%n%.txt" goto :loop
type nul > "%baseName%%n%.txt"
echo "%baseName%%n%.txt" was created
pause

Question title is about 'Increment text filename using batch script', so I dropped the timestamp in filename and just increment the name.
Here is my script:
#echo off
echo.
setlocal
set N=0
set FILENAME=baseFileName.%N%.anyExtension
:loop
set /a N+=1
set FILENAME=baseFileName.%N%.anyExtension
if exist %FILENAME% goto :loop
echo You can safely use this name %FILENAME% to create a new file
endlocal
Credit:
I created my own flavor based on #foxidrive's answer, meaning 99% credit goes to her/him

Related

How to remove names from a list in batch

I'm using windows bath, I have a list of names that I can add to but I don't know how to remove a name from the list.
So far my code is:
#echo off
setlocal enabledelayedexpansion
set allchoices=123456789
set "names=Bob,Steven,Harry"
set amount=6 ::max limit of list
set list=0
:start
::echoes a list of all names in the list
for /l %%i in (1; 1; %amount%) do (
call :sub %%i
)
goto check
:sub
for /f "tokens=%1 delims=," %%a in ("%names%") do (
echo %%i. %%a
set /a list=list+1
)
goto :eof
:check
::Remove a name from the list
choice /c !allchoices:~0,%list%! /m "What name do you want to remove?"
if errorlevel 3 (
for /f "tokens=3 delims=," %%a in ("%names%") do (
echo you have choosen to remove %%a
::remove third name in the list
goto start
)
)
if errorlevel 2 ::remove second name in the list
if errorlevel 1 ::remove first name in the list
I've tried using del but that turns out to delete a file in your folder.
I've tried renaming a specific name using set name[%%a]="" but that did nothing.
Any ideas?
Have a look at this example. There are many ways.
#echo off
setlocal enabledelayedexpansion
set names="Bob","Steven","Harry"
for %%i in (%names%) do (
set /a num+=1
set "!num!=%%~i"
)
for /l %%a in (1,1,%num%) do (
set choices=!choices!%%a
echo !num!.!%%a!
)
choice /c 123 /m "please select name to remove"
for /l %%a in (1,1,%num%) do if not "!%%a!"=="!%errorlevel%!" set new_names=!new_names! !%%~a!
echo %new_names:~1%
It can be done without the last for loop as well.. but I opted for it.
Here is some example code, for you to run, and then try to comprehend, I hope it helps rather than confuses:
#Echo Off
SetLocal EnableDelayedExpansion
For /F "Delims==" %%A In ('Set # 2^>NUL') Do Set "%%A="
Set "i=0"
For /F "Delims=:" %%A In ('FindStr "^::" "%~f0" 2^>NUL') Do (
Set /A i+=1
Set "#!i!=%%A"
Echo= !i!. %%A
)
:Ask
Set # 1>NUL 2>&1
If ErrorLevel 1 Echo= Empty names list&Timeout 3 1>NUL&Exit /B
Echo=&Set /P "Option= Choose a name to remove>"
Set #|Findstr "^#%Option%=" 1>NUL||GoTo :Ask
Set "Name=!#%Option%!"
Echo= You've chosen to remove "%Name%"
Timeout 2 1>NUL
Set "#%Option%="
ClS
For /F "Tokens=1*Delims=#=" %%A In ('Set # 2^>NUL') Do Echo= %%A. %%B
GoTo Ask
::Alan
::Beth
::Cleo
::Dale
::Eric
::Faye
::Greg
::Hugh
::Inga
Important note:Please ensure, before saving the above content as a Windows Command Script, that there is a line return, (blank line), at the end.

replacing a dynamic string with 'comma' - Windows batch file

I am trying to write a windows Batch file to obtain below OUTPUT for given INPUT
INPUT:
//Dev-420/PAVAN/src/main/java/test/abcd/mnop/HealthCheck.java - edit change 1111111
//Dev-420/PAVAN/src/main/java/test/abcd/mnop/HealthStatus.java - edit change 1111111
//Dev-420/PAVAN/src/main/java/test/xyz/Relations.java - edit change 1111111
OUTPUT:
target/classes/test/abcd/mnop/, target/classes/test/abcd/mnop/, target/classes/test/xyz
Below is the script I used, however replacing filename is not working.
#echo off
set "File2Read=files_list.tmp"
SET "BINARY_PATH="
set "FILENAME="
setlocal EnableDelayedExpansion
set "BINARY_FILENAME="
set "lastPart="
set "replaceBinaryPath=target/classes/"
SET "replaceBinaryFileName=,"
set "basePath=//ATT/Dev-420/PAVAN/src/main/java/"
for /f "delims=" %%a in ('Type "%File2Read%"') do (
set "line=%%a"
for /f "tokens=1,2,3,4,5 delims= " %%a in ("!line!") do set FILENAME=%%a
set "BINARY_FILENAME=!FILENAME:%basePath%=%replaceBinaryPath%!"
if not !FILENAME!==!BINARY_FILENAME! (
for %%a in ("!BINARY_FILENAME!/.") do set "lastPart=%%~nxa"
set "BINARY_PATH=!BINARY_FILENAME:!lastPart!=%replaceBinaryFileName%!"
echo !BINARY_PATH!
)
)
endLocal
here, everything works as expected, except below statement,
set "BINARY_PATH=!BINARY_FILENAME:!lastPart!=%replaceBinaryFileName%!"
Since ‘lastPart’ will be the dynamically changing-value, in order to replace it with ‘comma’ It was supposed to work with !lastPart! but its not working and output is word ‘lastPart’
We cant use %lastPart% as it is dynamic variable.
Please help me with replacing a dynamic string with 'comma'.
Please help me out here!
You could use the %%~nxa expression directly, I only changed the parameter name to %%C
..
for /f "delims=" %%a in ('Type "%File2Read%"') do (
set "line=%%a"
for /f "tokens=1,2,3,4,5 delims= " %%a in ("!line!") do set FILENAME=%%a
set "BINARY_FILENAME=!FILENAME:%basePath%=%replaceBinaryPath%!"
if not !FILENAME!==!BINARY_FILENAME! (
for %%C in ("!BINARY_FILENAME!/.") do (
set "BINARY_PATH=!BINARY_FILENAME:%%~nxC=%replaceBinaryFileName%!"
echo !BINARY_PATH!
)
)
)
As #jeb's version didn't work for me, here an alternative
as the usage of %%~dpa changes the the non windows path separator / to \ it is corrected.
:: Q:\Test\2018\09\24\SO_52482593.cmd
#echo off & setlocal EnableDelayedExpansion
set "File2Read=files_list.tmp"
set "BINARY_PATH="
set "replaceBinaryPath=target/classes/"
set "replaceBinaryFileName=, "
set "Output="
for /f %%a in ('Type "%File2Read%"') do (
for /f "tokens=5* delims=\/" %%b in ("%%~dpa") do (
Set "Output=!Output!%replaceBinaryFileName%%replaceBinaryPath%%%c"
)
)
Set "Output=%Output:\=/%"
Echo %Output:~2%
endLocal
Sample output:
> Q:\Test\2018\09\24\SO_52482593.cmd
target/classes/test/abcd/mnop/, target/classes/test/abcd/mnop/, target/classes/test/xyz/

Use a Batch File to list files and allow the user to select which file to copy into a new destination

I am a newbie to Windows Scripting.
I am trying to list some txt files in several sub directories & want to copy a user selected file to a new destination. Please note that the file name is unique in different locations.
I got the first part to work (Listing out the files & locations) using the following script, but I am unable to copy the selected file to the new location.
#ECHO OFF
SET index=1
SETLOCAL ENABLEDELAYEDEXPANSION
SET FFPath="C:\Scripts - Backup Server\DKXpress_bkp"
SET NewPath=C:\DKServer
ECHO Recursively searching %FFPath%
echo.
FOR /F "delims=" %%f in ('DIR %FFPath%\*.txt /a:-d /s /b') DO (
SET file!index!=%%f
ECHO !index! - %%f
SET /A index=!index!+1
)
SETLOCAL DISABLEDELAYEDEXPANSION
SET /P selection="select file by number:"
SET file%selection% >nul 2>&1
IF ERRORLEVEL 1 (
ECHO invalid number selected
EXIT /B 1
)
SET NewFile=file%selection%
ECHO Copying %NewFile% to %NewPath%
ECHO.
COPY /Y "%NewFile%" "%NewPath%"
ECHO.
PAUSE
I think I am doing this part wrong
SET NewFile=file%selection%
Thank you all in advance
You don't need to set an index variable or delayed expansion, if you let Find do the work for you:
#Echo Off
Set "FFPath=C:\Scripts - Backup Server\DKXpress_bkp"
Set "NewPath=C:\DKServer"
Echo Recursively searching %FFPath%
Echo=
For /F "Delims==" %%A In ('"Set File[ 2>Nul"') Do Set "%%A="
For /F "Tokens=1* Delims=]" %%A In (
'"Dir /B/S/A-D-S-L "%FFPath%\*.txt" 2>Nul|Find /N /V """') Do (
Echo %%A] %%B
Set "File%%A]=%%B"
)
Echo=
Set /P "#=Select file by number: "
Echo=
For /F "Tokens=1* Delims==" %%A In ('"Set File[%#%] 2>Nul"') Do (
Echo Copying %%B to %NewPath%&Echo=
Copy /Y "%%B" "%NewPath%"
GoTo :End
)
Echo Invalid number selected
:End
Echo=
Pause
You need to use delayed expansion to get the file name assigned to the variable correctly.
SET NewFile=!file%selection%!
Remove the setlocal to disable delayed expansion.
You can try something like that :
#ECHO OFF
:Main
cls
SET index=1
SETLOCAL ENABLEDELAYEDEXPANSION
SET FFPath="C:\Scripts - Backup Server\DKXpress_bkp"
SET "NewPath=C:\DKServer"
ECHO Recursively searching %FFPath%
echo.
FOR /F "delims=" %%f in ('DIR %FFPath%\*.txt /a:-d /s /b') DO (
SET filepath[!index!]=%%f
ECHO [!index!] - %%~nxf - %%f
SET /A index=!index!+1
)
echo(
echo select file by number :
set /p Input=""
For /L %%i in (1,1,%index%) Do (
If "%INPUT%" EQU "%%i" (
ECHO Copying "!filepath[%%i]!" to "!NewPath!"
COPY /Y "!filepath[%%i]!" "!NewPath!"
)
)
echo Copying another file ? (Y = Yes or N = No) ?
set /p input2=""
If /I "!input2!"=="Y" (
goto :Main
) else (
goto :eof
)

Output filename and count of a directory in a text file - windows

I have a batch to output filename and count of filenames with the same prefix and date in a text file.
E.g.
Q1231111.zip
Q1241111.zip
where:
Q123 - is a prefix
1111 - is a date.
I want an output such as:
123 : 1
124 : 1
125 : 0
But the batch file unable to output the last one. I wanna see the file is present so i need the 0 output.
Here's my code:
#echo off
setlocal EnableExtensions
for %%I in ("Z:\StoreDataJDA\Q1231111.zip") do call :CountFile "%%~nI"
for %%I in ("Z:\StoreDataJDA\Q1241111.zip") do call :CountFile "%%~nI"
for %%I in ("Z:\StoreDataJDA\Q1251111.zip") do call :CountFile "%%~nI"
for /F "tokens=2,3 delims=#=" %%I in ('set Group# 2^>nul') do echo %%I: %%J >>count.txt
endlocal
goto :EOF
:CountFile
set "FileName=%~1"
set "FileGroup=%FileName:~1,4%"
if "Group#%FileGroup%" == "" (
set "Group#%FileGroup%=1"
) else (
set /A Group#%FileGroup%+=1
)
goto :EOF
pause
Thanks in advance!
From your description, it's hard to tell exactly what you are looking for. But by looking at your very similar question from a year ago, and from Gerhard's comment, I think what you're looking for is:
Count the number of files with a given prefix, each file having a different date suffix.
I think this should do it:
#echo off
setlocal EnableExtensions
set "Group#123=0" & for %%I in ("Z:\StoreDataJDA\Q123????.zip") do call :CountFile "%%~nI"
set "Group#124=0" & for %%I in ("Z:\StoreDataJDA\Q124????.zip") do call :CountFile "%%~nI"
set "Group#125=0" & for %%I in ("Z:\StoreDataJDA\Q125????.zip") do call :CountFile "%%~nI"
(for /F "tokens=2,3 delims=#=" %%I in ('set Group# 2^>nul') do echo %%I: %%J)>count.txt
endlocal
goto :EOF
:CountFile
set "FileName=%~1"
set "FileGroup=%FileName:~1,3%"
set /A Group#%FileGroup%+=1
goto :EOF

How to create a unique output filename for Windows Script?

I am trying to create a windows script that should generate this kind of filename everytime I run it: filename1, filename2, filename3 and so on. Here is what I have so far:
(
#echo off
wmic logicaldisk get size,freespace,caption
) > disk.txt
I hope you can help me. Thanks!!
:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")
You now have any number of filenames available.
%tempfile%a exists and is empty, but %tempfile%anythingelse should be available for use.
#ECHO OFF
SETLOCAL
SET "basename=filename"
SET /a outname=0
:genloop
SET /a outname+=1
IF EXIST "%basename% %outname%.txt" GOTO genloop
SET "outname=%basename% %outname%.txt"
ECHO %outname%
GOTO :EOF
Ah - increment the destination filename on each run. This should do that. It's not actually creating a file - you'd need to create the file %outname% each time to have it increment...
(the space between %basename% and %outname% is optional, of course - omit it if desired.)
edited to include .txt
This will give you up to 1000 filenames but you can go higher, up to 2 Billion, but the higher you go the longer the delay will be before it picks a filename.
#echo off
for /L %%a in (1,1,1000) do if not defined filename if not exist "filename%%a.txt" set "filename=filename%%a.txt"
(
wmic logicaldisk get size,freespace,caption
) > "%filename%"
#echo off
setlocal enableextensions
call :getNextFilename "filename*.txt" nextFilename
echo %nextFilename%
echo test > "%nextFilename%"
call :getNextFilename "%cd%\filename*.txt" nextFilename
echo %nextFilename%
echo test > "%nextFilename%"
endlocal
exit /b
:getNextFilename whatToSearch returnVariable
setlocal enableextensions enabledelayedexpansion
for /f %%a in ("$\%~1"
) do for /f "tokens=1,* delims=*" %%b in ("%%~nxa"
) do ( set "left=%%b" & set "right=%%c" )
set "max=0"
for %%a in ("%~1"
) do for /f "tokens=1 delims=%left%%right% " %%b in ("%%~nxa"
) do for /f "tokens=* delims=0 " %%c in ("0%%~b"
) do if %%~c geq !max! set /a "max=%%c+1"
endlocal & set "%~2=%~dp1%left%%max%%right%" & exit /b
This should find the next file in sequence independently of the existence of holes in the numeration of the files. A path can be included or omitted. The * will be used as the placeholder for the numeration. BUT this will not work if files or included paths have "problematic" characters.
If the date/time of creation of the file can be considered, then this version can be optimized as
:getNextFilename whatToSearch returnVariable
setlocal enableextensions disabledelayedexpansion
for /f %%a in ("$\%~1"
) do for /f "tokens=1,* delims=*?" %%b in ("%%~nxa"
) do ( set "left=%%b" & set "right=%%c" )
set "max=0"
for /f "delims=" %%a in ('dir /tc /o-d /b "%~1" 2^>nul'
) do for /f "tokens=1 delims=%left%%right% " %%b in ("%%~nxa"
) do for /f "tokens=* delims=0 " %%c in ("0%%~b"
) do set /a "max=%%c+1" & goto done
:done
endlocal & set "%~2=%~dp1%left%%max%%right%" & exit /b
that will take the latest created instance of the file set.
I finally figured out where to put the .txt extension. This is from #Magoo's answer but I wanted the file to be a text file so I placed the .txt twice in order for it to work properly.
#ECHO OFF
SETLOCAL
SET "basename=DISK-OUT"
SET /a outname=0
:genloop
SET /a outname+=1
IF EXIST "%basename% %outname%.txt" GOTO genloop
SET "outname=%basename% %outname%.txt"
(
wmic logicaldisk get size,freespace,caption
) > "%outname%"
GOTO :EOF

Resources