I've been looking for a .bat file in Windows 7 that given a name and a file directory, renames all of the files in that directory to the name that the user pass concatenated to a number that goes from 0 to 9.
For example:
Let's say that i have a directory with two files a.txt and b.txt, i want to change their names to documentX.txt (X being a number from 0 to 9), so at the end the script will change the names to document1.txt and document2.txt
i have tried something like this but without sucess:
#echo off
#set a=1
#set pref=hola
for /D %%f in (C:\*.txt)
do
rename *.txt %pref%%a%
#set /A a = %a%+1
The FOR command cannot span multiple lines unless you use parentheses or line continuation ^. The first opening parenthesis must be on the same line as the IN and the 2nd on the same line as the DO.
You cannot use normal expansion within a parenthesised block of code that also sets the value because the expansion occurs at parse time and the entire block is parsed at once. So the value will be the value that existed before you set it! The solution is to use delayed expansion. That occurs when the line is executed. It must be enabled with setlocal enableDelayedExpansion.
Delayed expansion causes a problem because ! is valid within a file name and FOR variable expansion will be corrupted if it contains ! while delayed expansion is enabled. The solution is to toggle delayed expansion on and off within the loop.
You want a simple FOR without any options. The /D option looks for directories instead of files.
You do not need to expand numeric variables within a SET /A statement.
One last thing - no need for # after you use ECHO OFF.
#echo off
setlocal disableDelayedExpansion
set "n=0"
set "pref=document"
for %%F in (c:\*.txt) do (
set "file=%%F"
set /a n+=1
setlocal enableDelayedExpansion
ren "!file!" "%pref%!n!.txt"
endlocal
)
There is a simpler way to accomplish the task. Use DIR /B to list all the .txt files and pipe the results to FINSTDR /N "^". The FINDSTR will match all files and will prefix each value with a sequential number followed by a colon. Use FOR /F to parse the result into the number and the file name so you can then RENAME.
#echo off
setlocal
set "pref=document"
for /f "tokens=1* delims=:" %%A in ('dir /b /a-d *.txt^|findstr /n "^"') do (
ren "%%B" "%pref%%%A.txt"
)
Related
I"m trying to delete these files with numbers appended to them using a for loop in a Windows batch file.
My problem is that I can't seem to build the filename strings using the for loop, despite several attempts at doing this.
Below is the snippet of my code that I"m trying to run with. As you can see, I've got 4 files named file0.txt, file1.txt, file2.txt and file3.txt in some nested folder, and I'm trying to delete them using a for loop
Ideally I want to be able to be able to set the limit of the for loop depending on how many files there are. And then I'd want to change the file extension from .txt to whatever to delete other files except the ones I want to keep.
Any help would be appreciated!! Here's the code:
setlocal enabledelayedexpansion
set num=3
set /a forLoopLimit=%num%-1
cd folder
FOR /L %%x IN (0,1,%forLoopLimit%) DO (
setlocal enabledelayedexpansion
echo %x
set y=%x
set filename=file%y%.txt
del %filename% /f /q
echo %filename%
)
cd ..
setlocal enabledelayedexpansion
set num=3
set /a forLoopLimit=%num%-1
cd folder
FOR /L %%x IN (0,1,%forLoopLimit%) DO (
echo %%x
set filename=file%y%.txt
ECHO del file%%x /f /q
echo file%%x
)
cd ..
Problems:
To refer to the metavariable (Loop-control variable) x, you need %%x, not %x within a batch file.
setlocal is not a switch. Each time it is used, it establishes a new frame which is terminated by endlocal or reaching end-of-file.
If you are using delayededexpansion then you need to refer to the variable you are changing using !var!, not %var%. !var! means the changed value, %var% means the value of the variable as it was when the for keyword was encountered.
You don't need to use y in your application. %%x is actually a string, but it will be a numeric string, so it can be used in calculations.
The del command is simply echoed above to allow the command to be displayed - in case there's a code error which might delete unexpectedly.
Basically, you fell into the delayed expansion trap (you enabled delayed expansion, but you didn't use it).
But your task can be done without those variables that need delayed expansion. You can use %%x directly and define the rest outside the loop:
#echo off
set num=3
set /a forLoopLimit=%num%-1
set "filebase=file"
pushd folder
FOR /L %%x IN (0,1,%forLoopLimit%) DO (
ECHO del "%filebase%%%x.txt" /f /q
)
popd
Like Magoo, I disarmed the del command by just echoing it. If the output is what you expect, just remove the ECHO.
Need help ! Working on a batch file which will replace a set of characters from a bunch of text files in a folder. I have found the code which will do that. But it does for only one file. IS there a way where it can do it for all the files in the folder. There a total of 1000 files inside the folder. I am using a Windows 7 OS. Attaching the code I found where it does for one file.
Thanks
Harry
#echo off
setlocal enabledelayedexpansion
set INTEXTFILE=Replace_string.txt
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=Apple
set REPLACETEXT=Mango
set SEARCHTEXT=Cat
set REPLACETEXT=Dog
set OUTPUTLINE=
for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do (
SET string=%%A
for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
if "!string!" == "" (
echo.>>%OUTTEXTFILE%
) else (
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !modified! >> %OUTTEXTFILE%
)
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%
If you want a pure batch solution, then the simplest thing to do is to encapsulate the code in a subroutine that takes the name of the file as an argument, and call that routine from within a FOR loop that iterates the file names. This is a general approach that can be used for any code that you want to run iteratively
against files in a folder.
The end result of your code does not create or rename any folders, so a simple FOR is safe to use. But if your code creates or renames folders, then a FOR loop could also process the newly created or renamed files. This can be solved by using FOR /F with the DIR /B command instead.
Note - I eliminated dead (unused) variables from the code.
#echo off
setlocal enabledelayedexpansion
pushd "c:\path\to\your\folder\containing\files\to\modify"
for %%F in (*.txt) do call :replace "%%F"
exit /b
:replace
set INTEXTFILE=%1
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=Cat
set REPLACETEXT=Dog
for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do (
SET string=%%A
for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
if "!string!" == "" (
echo.>>%OUTTEXTFILE%
) else (
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !modified! >> %OUTTEXTFILE%
)
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%
exit /b
But there are many limitations and inefficiencies with the code, with lots of room for improvement.
Limitations:
Input and output lines must be a bit less than 8191 bytes long.
The search ignores case
The search string cannot contain = or begin with ~, * or !
The replace string cannot contain !
Lines containing ! will be corrupted because delayed expansion is enabled when %%A is expanded. This can be solved by strategically toggling delayed expansion on and off within the loop(s).
Leading : will be stripped from all lines because consecutive delimiter characters are treated as a single delimiter.
The replacement will be corrupted if the search term contains %%a or %%b or %%A. This can be avoided by transferring the search and replacement terms to FOR variables.
Certain characters within the search and/or replacement terms could cause problems or require complex escape sequences. This can be simplified by getting the desired strings in environment variables (which may still require escape sequences) and then using delayed expansion and FOR /F to transfer the values to FOR variables.
There are obscure situations where ECHO. can fail. The only safe variant that is guaranteed to work is ECHO(.
A non empty line could become empty after replacement if the replacement string is empty, and the empty line will not be output properly because neither ECHO. nor ECHO( was used.
Inefficiencies / other issues
Redirection is performed for each line of output, which is slow. Better (faster) to redirect once outside the loop.
The DEL/RENAME pair can be replaced by a single MOVE command
CALL is relatively slow. Best to minimize use of CALL if you want the fastest possible solution. But sometimes it cannot be avoided.
I prefer to have my temp file name to be a derivative of the original name. I typically append a .new extension to the original name, so "original.txt" becomes "original.txt.new"
Below is highly optimized code that addresses all but the first 4 points above. It is about as robust and efficient as pure batch can get if you want to use FOR /F to read the file.
#echo off
setlocal disableDelayedExpansion
pushd "c:\path\to\your\folder\containing\files\to\modify"
set "find=Cat"
set "repl=Dog"
setlocal enableDelayedExpansion
for /f delims^=^ eol^= %%S in ("!find!") do (
for /f delims^=^ eol^= %%R in ("!repl!") do (
endlocal
for %%F in (*.txt) do (
for /f "delims=" %%L in ('findstr /n "^" "%%F"') do (
set "ln=%%L"
setlocal enableDelayedExpansion
set "ln=!ln:*:=!"
if defined ln set "ln=!ln:%%S=%%R!"
echo(!ln!
endlocal
)
) >"%%F.new" & move /y "%%F.new" "%%F" >nul
)
)
popd
The above required a lot of experience and arcane knowledge to develop. Even after all the work, it still has the following limitations.
Input and output lines must be a bit less than 8191 bytes long.
The search ignores case
The search string cannot contain = or begin with ~, * or !
The replace string cannot contain !
Removing those limitations would require a ridiculous amount of slow and even more impenetrable code. Hardly worth it.
That is why I have abandoned using pure batch for text processing, and I developed JREPL.BAT to provide powerful regular expression text processing capabilities to the Windows command environment. JREPL is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.
JREPL makes the solution so simple, it can easily be run directly on the command line, without any batch file.
pushd "c:\your\path\with\files\to\be\modified"
for %F in (*.txt) do call jrepl "Cat" "Dog" /l /f "%F" /o -
popd
The search is case sensitive. You can add the /i option if you want the search to ignore case. Change the %F to %%F if you use the command within a batch script.
Since all you need to do is replace text in an existing file, use a tool like FART which is specifically designed to do this:
FART -r "C:\Data\Directory\*.txt" "OldText" "NewText"
The -r switch says to process all txt files in provided directory and subfolders.
Note that you can add the -i switch to ignore the case when searching.
I am trying to output a file (count.txt) with the name of each csv and tab file in the current directory and the number of lines in that file. Although the tab file needs its value divided by 4. Also, I would like to get rid of the 10 dashes 'find' outputs and maybe rework the filename. How do i grab the out put of find and manipulate it?
del count.txt
for %%f in (*.csv) do (
find /c /v "" %%f >> count.txt
)
for %%f in (*.tab) do (
::need to divide the next value by 4
find /c /v "" %%f >> count.txt
)
edited to correct errors and adapt to comments
#echo off
setlocal enableextensions disabledelayedexpansion
rem Define extensions to handle
set "extensions=.tab .csv"
rem Define dividers for each extension
set ".tab=4"
set ".csv=1"
rem Retrieve starting folder from command line
rem If not present, assume current folder
set "root=%~1" & if not defined root set "root=%cd%"
rem For each folder starting at the indicated root
rem Change current path to the folder that will be processed and
rem (%%X) For each extension in list
rem (%%a/%%b) For each matching file/lines number processed by find command
rem Calculate the number of lines
rem Echo file information
rem Return to previous active directory
(for /r "%root%" /d %%F in (.) do (
pushd "%%~fF" && (
for %%X in (%extensions%) do if exist ".\*%%~X" (
for /f "tokens=2,3 delims=\:" %%a in ('
find /c /v "" ".\*%%~X" 2^>nul
') do (
set /a "numLines=%%b / %%~xa"
setlocal enabledelayedexpansion
for %%c in (!numLines!) do (
endlocal
echo [%%~fa] [%%c]
)
)
)
popd
)
)) > count.txt
Where the task of each of the for loops is
%%F will recurse the folder hierarchy from the indicated starting point
%%X will iterate over the list of extensions to handle
%%a will execute the find command over all the indicated files and split each line using a backslash and a colon. The slash is included in the output of the find command because we use a relative reference to each file (.\*.csv). That way the initial dashes and space are removed. Then the colon is used to separate the file name from the line count. At the end %%a will hold the file name and %%b the line count.
Once we have the required elements, we will need delayed expansion active to access the result of the calculation. The file extensions is used to determine the divider using variables defined for each case.
%%c Having delayed expansion active generates a problem with files that contain an ! it its name, so we need to disable (endlocal) the delayed expansion before executing the echo, so an aditional for command is used to hold the value of the number of lines that need to be echoed. With delayed expansion disabled, the information is echoed.
The outer for loop is enclosed in parenthesis so we can redirect all the output opening the output file only once instead of one open/write operation for each file.
I am using Windows XP and need to create a batch file to move tok.filename to filename.tok.
This works for one file if I type in fix.bat tok.filename.
set filename=%1
set newname=%filename:~4,45%
ren %1 %newname%.tok
I need to type in fix.bat tok*, but this puts tok* in the filename.
How do I get it to read all the files into the filename one at a time?
Use a for statement.
setlocal enabledelayedexpansion
for %%i in (tok.*) do (
set filename=%%i
set newname=!filename:~4,45!
ren %%i !newname!.tok
)
Enabling the delayed expansion makes it so that the variables are evaluated at the time they are used.
Alternatively, since you already have a batch file that works, you could write another batch file that uses the for statement, which calls your working batch file -- like this:
for %%i in (tok.*) do call fix.bat %%i
Or you could run it directly from the command line like this:
for %i in (tok.*) do call fix.bat %i
No need for a batch script
for %F in (tok.*) do for /f "delims=." %X in ("%~xF") do ren "%F" "%X.%~nF"
Double up the percents if used within a batch script.
The above may not give the desired result if more than one . appears in a file name.
For example tok.part1.part2 would become part2.tok.part1.
If you want part1.part2.tok instead, then you can use the following simple script.
#echo off
ren tok.* .*?.tok
for %%F in (.*.tok) do for /f "tokens=* delims=." %%A in ("%%F") do ren "%%F" "%%A"
For an explanation of how that 1st REN works in the 2nd script, see How does the Windows RENAME command interpret wildcards?
I suppose an esoteric argument could be made that a name like tok..name would become name.tok when it should be .name.tok. That could be fixed by using:
#echo off
setlocal disableDelayedExpansion
ren tok.* .*?.tok
for %%F in (.*.tok) do (
set "name=%%F"
setlocal enableDelayedExpansion
ren "!name!" "!name:~1!"
endlocal
)
The nice thing about the solutions above is they don't rely on the length of "tok". A string value of any length could be substituted for "tok" and the code works just as well.
The James L solution works well for a constant length. That solution can be improved a bit.
The 2nd argument to the substring operation is not needed. Removing it lets the solution work no matter how long the full original name is.
The code will fail if a name has an ! in it. Fixed by toggling delayed expansion on and off.
The filenames should be quoted in the REN statement in case of spaces or special characters
.
#echo off
setlocal disableDelayedExpansion
for %%i in (tok.*) do (
set "filename=%%i"
setlocal enableDelayedExpansion
ren "!filename!" "!filename:~4!.tok"
endlocal
)
you can also do the same without delayed expansion (which is slow).
Also, if there is only one dot, without explicitly using lengths of name parts.
%~n expands to filename without extension, and %~x to dot with extension (so I remove dot with :~1).
Also, using %* in for, you may pass arbitrary number of arguments.
And even more. If you add /R bewteen for and %%I (you'll get for /R %%I), it will recurse into subdirs also.
Whole batch file:
for %%I IN (%*) do call :Rename "%~I"
exit /b
:Rename
set newname=%~x1
ren %1 "%~dp1%newname:~1%.%~n1"
exit /b
I am trying to get a batch to file read a text file from dropbox and execute it as variables in the batch file.
this is what i am trying, but it does not work, please help!
SetLocal EnableDelayedExpansion
set content=
for /F "delims=" %%i in (DROPBOX-LINK-HERE) do set content=!
content! %%i
%content%
EndLocal
I'm not sure what you mean by DROPBOX-LINK-HERE, but I am using an ordinary text file for content.
You must separate each line with & or else enclose the content in parentheses and separate each line with <linefeed>. The linefeed solution is more complicated, but has fewer limitations on the content.
Any ! characters in the content will be corrupted during expansion of a FOR variable if delayed expansion is enabled. But delayed expansion is needed to preserve unquoted special characters. So delayed expansion needs to be creatively toggled on and off.
Here is code that I think does what you want.
#echo off
setlocal disableDelayedExpansion
::Define a carriage return variable
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
::Create a newline variable
set LF=^
::The above 2 blank lines are critical - do not remove
::Both CR and LF should be expanded using delayed expansion only.
::Load the content into a variable.
::We want to separate lines with linefeed, but FOR /F won't preserve linefeeds.
::So use carriage return as a place holder for now.
set "content=("
setlocal enableDelayedExpansion
for /f %%C in ("!CR! ") do (
endlocal
for /f "delims=" %%A in (test.txt) do (
setlocal enableDelayedExpansion
for /f "delims=" %%B in ("!content!") do (
endlocal
set "content=%%B%%C%%A"
)
)
)
::Now replace carriage returns with newline and append terminating )
setlocal enableDelayedExpansion
for /f %%C in ("!CR! ") do for %%N in ("!LF!") do set "content=!content:%%C=%%~N!%%~N)"
::Execute the content
endlocal&%content%
The code works, but there are limitations to the type of code that can be executed from a variable.
Variables cannot be expanded by using normal expansion unless you use CALL. For example, a line like echo %var% will not work, but call echo %var% will work. Another option is to use delayed expansion. SETLOCAL EnableDelayedExpansion and ENDLOCAL can be included in the content as needed.
You cannot CALL or GOTO a :LABEL within the content.
That's all I can remember at the moment, but there may be (probably are) other restrictions.
I have one question though:
If the content is already in a text file, then why not simply give the text file a .BAT extension and execute it?