To read lines from a file, in a batch file, you do :
for /f %%a in (myfile.txt) do (
:: do stuff...
)
Now suppose you file is in C:\Program Files\myfolder
for /f %%a in ("C:\Program Files\myfolder\myfile.txt") do (
echo %%a
)
Result :
C:\Program Files\myfolder\myfile.txt
This seems to interpret the given path as a string, and thus %%a is your given path.
Nothing about this in the documentation I have found so far.
Please someone help me before I shoot myself.
The documentation you get when you type help for tells you what to do if you have a path with spaces.
For file names that contain spaces, you need to quote the filenames with
double quotes. In order to use double quotes in this manner, you also
need to use the usebackq option, otherwise the double quotes will be
interpreted as defining a literal string to parse.
By default, the syntax of FOR /F is the following.
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
This syntax shows why your type workaround works. Because the single quotes say to execute the type command and loop over its output. When you add the usebackq option, the syntax changes to this:
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ('string') DO command [command-parameters]
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]
Now you double quote paths to files, single-quote literal strings, and put backticks (grave accents) around commands to execute.
So you want to do this:
for /f "usebackq" %%a in ("C:\Program Files\myfolder\myfile.txt") do (
echo %%a
)
Found it.
for /f %%a in ('type "C:\Program Files\myfolder\myfile.txt"') do (
echo Deleting: %%a
)
Don't even ask me why that works.
Just sharing the below code, hoping that somebody will get benefited.
The below code take both the path having spaces and also if the read lines has spaces, it wont cause any issue of the characters after space is missing;
FOR /f "tokens=* delims=," %%a in ('type "C:\Progrem File\My Program"') do (
echo %%a
)
Related
I have some file names as below saved in C:\aaa\temp
Before:
92485345_A0027777882244.zip
87493354_A0027684085444.zip
87111901_A0027871905777.zip
some fixed rule in the file name:
I need to delete the characters until the underscore "_"
The name after underscore always begins with A0027
Can someone please teach me how to write a script to batch rename them as below:
After:
A0027777882244.zip
A0027684085444.zip
A0027871905777.zip
If the number of characters in front of the underscore _ is always the same, then use this:
rem // Expecting 8 characters in front of `_`:
ren "????????_A0027*.zip" "/////////*.*"
This is an undocumented feature of ren, which is described there.
When the number of characters in front of _ can vary you have to use dir /B to get the list of matching files, a for /F loop to capture it and to split the file names, then ren to finally rename each file:
for /F "tokens=1* delims=_" %E in ('dir /B /A:-D "*_A0027*.zip"') do #ren "%E_%F" "%F"
To use this code in a batch-file you need to change % to %%:
for /F "tokens=1* delims=_" %%E in ('dir /B /A:-D "*_A0027*.zip"') do (
ren "%%E_%%F" "%%F"
)
List the files from the directory.
Iterate through the list of names.
Remove chars up until the underscore (including).
Rename each file to the new name as you iterate through list.
Now, if you chose to do this with a script or pipe through, it is up to you.
I want to, for example, replace all -v1 parts in every filename for -v2. It's almost working, but the access is denied because the files are being used by the cmd process itself during execution. My current code is:
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do ren "%%a" "%%a:-v1=-v2"
Any suggestions? :)
EDIT:
I've also tried for /r %%i in ("*-v1*") do ren "%%~nxi" "%%~nxi:-v1=-v2" and it finds the files and uses the correct rename value, but still outputs that it can't access the file because it is in use by another process. I'm sure that process is the cmd.exe itself, because after I close the command prompt, I can change the filenames manually without any problems.
I also tried by writing the current filenames to a temporary .txt file with the idea that the files I want to rename aren't used by any command. Then read the file with the type command within a for loop to rename each file, but same thing.
It's quite frustrating, any help is appreciated :)
substring substituion doesn't work with for variables (%%a). Use a "normal" variable and delayed expansion:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do (
set filename=%%a
ren "%%a" "!filename:-v1=-v2!"
)
I have some files with different names.
Leviathan.txt,Dragon.txt and so on
I wanted to turn it into a digit begins
1.txt,2.txt,3.txt,4.txt and so on
how to perform like other language by using For and function that can pass amount files in folder?
my code so far i know is dir and ren. and i stuck now.
ren *.txt 1.txt
Next code snippet could work for you (save with .bat extension); note that rename command is echoed merely for debugging purposes:
#echo off
SETLOCAL enableextensions enabledelayedexpansion
set /A "ii=0"
pushd "working_directory_here"
for /F "delims=" %%G in ('dir /B /ON "*.txt" 2^>NUL') do (
set /A "ii+=1"
echo ren "%%~G" "!ii!%%~nxG"
)
popd
If you insist on an one-liner (launch in proper working directory):
cmd /E:ON /V:ON /K (#echo off^&set /A "ii=0" ^>NUL^&for /F "delims=" %G in ('dir /B /ON "*.txt" 2^^^>NUL') do (set /A "ii+=1" ^>nul^&echo ren "%~G" "!ii!%~nxG"))^&exit
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(%~G etc. special page) Command Line arguments (Parameters)
(EnableDelayedExpansion) Setlocal EnableDelayedExpansion
(^>, %% etc.) Syntax : Escape Characters, Delimiters and Quotes
Assuming none of your existing files are already named something like n.txt, where n is a number, then simply CD to your folder, and run the following command from the command line:
for "tokens=1* delims=:" %A in ('dir /b *.txt^|findstr /n "^"') do #ren "%B" "%A.txt"
Double up the percents if you use the command within a batch script.
EDIT
I forgot about my JREN.BAT utility - a regular expression renaming utility. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.
JREN has a built in ability to incorporate a number into each new file name, and as an added bonus, it can left pad the number with zeros so that a DIR command lists the files in numerical order. The default numeric width is 3 digits, so files would be like "001.txt", "002.txt', ... "010.txt", ... "100.txt", etc.
jren "^.*" "$n+'.txt'" /j /fm *.txt
The /NPAD option specifies the minimum numeric width, so NTAB 1 produces no padding, which is what the original question asked for.
jren "^.*" "$n+'.txt'" /j /fm *.txt /npad 1
Since JREN is a batch script itself, you must use CALL JREN if you put the command within another batch script.
Full documentation is available from the command prompt via jren /? | more. My console window is configured with a large buffer, so I can scroll back to see prior output, and I don't bother with piping the help to MORE.
i have a text file datefile.txt that contains
10-06-2013
and I tried to read it using the following bat file:
#echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ datefile.txt"`) do (
set "var=%%a"
SETLOCAL EnableDelayedExpansion
set "var=!var:*:=!"
echo(!var!
ENDLOCAL
)
echo %var%
the output I got are these:
10/06/2013
1:10/06/2013
how come my %var% is different from above one.
Or how could I remove "1:" in the %var%?
thanks.
You got this type of output, as the first line is written by echo(!var!.
The second line by echo %var%, but in the second case the variable doesn't contain the same.
This is because the Setlocal/endlocal block inside the for loop.
In your case you can simply remove the block, as your date doesn't contains any exclamation marks nor carets.
#echo off
SETLOCAL EnableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ datefile.txt"`) do (
set "var=%%a"
set "var=!var:*:=!"
echo(!var!
)
echo %var%
And if your file contains only one line, the solution could be simplified by
<datefile.tx set /p var=
echo %var%
the solution is to remove the "1:" using dos substr replacement:
http://www.dostips.com/DtTipsStringOperations.php#Snippets.Remove
first of all im beginner. i want to create batch file to search through specific folder (including all it subfolder) and copy all file inside it except those which filename contain some specific string,this is what i have so far
set now=fish
set logDirectory="C:\Users\paiseha\Desktop\bb\"
for /r %logDirectory% %%i IN (*%now%*.*) do (
rem copy process goes here
)
let say i have 3 file in it
C:\Users\fareast\Desktop\bb\one.txt
C:\Users\fareast\Desktop\bb\twofishtwo.txt
C:\Users\fareast\Desktop\bb\three.txt
so i want to copy file one.txt and three.txt only, but instead it copy only the second one,i know its because of *%now%*.* so how can i invert it so that it does the other way around, help me pls, thanks in advance
try:
#ECHO OFF &setlocal
set "now=fish"
set "logDirectory=C:\Users\paiseha\Desktop\bb"
for /f "delims=" %%a in ('dir /a-d/b/s "%logDirectory%"^|findstr /riv "^.*[\\][^\\]*%now%[^\\]*$"') do (
rem copy process goes here
)
EDIT: The \ character is represented as [\\] instead of \\ because of a quirk on how Vista FINDSTR regex escapes \. Vista requires \\\\, but XP and Win 7 use \\. The only representation that works on all platforms is [\\]. See What are the undocumented features and limitations of the Windows FINDSTR command? for more info.
for /f "delims=" %%a in ('dir /a-d/s/b "%logDirectory%" ') do echo %%~nxa|findstr /i /L "%now%" >nul&if errorlevel 1 ECHO COPY "%%a"
should work for you.