Currently I just success to read from one .txt file only. Here is my code.
for /f "delims=" %%a in (C:\test\Scriptlogs\COB\log_DP_20140331_1509_CW52.txt) do SET e=%%a
My question is I got one folder contain of many .txt file, however above code only read specified .txt file. Any changes that I need to made so that it can loop through all the .txt files?
Example of .txt file name.
log_DP_20140331_1324_CW52.txt
log_DP_20345692_1234_CW51.txt
log_DP_21234324_2134_CW50.txt
FYI, folder contains more than 3 .txt files.
Any guidance ,answer or similar post to share?
Thanks for viewing, comments and answers.
You have how to iterate over a file content. The only you need is to iterate over the file list and read the content of each file. So
for %%x in (C:\test\Scriptlogs\COB\log_DP_*.txt
) do for /f "usebackq delims=" %%a in ("%%~fx") do .....
Where %%x holds a reference to each file in the set and %%~fx is the full path to the file. To avoid problems with possible spaces in filenames, i have quoted it "%%~fx", so, to indicate to for /f comand that it is not a string but a file, usebackq has been included in the for options string
What you need is the forfiles function. You can put the code you have into the body of the forfiles loop. Check out ss64.com it is a great resource for a number of scripting languages.
Related
I didn't find so far a similar question, so I try to explain the problem:
I have large number of files that are in subfolders inside "C:\images"
I have a list of names in two columns, so that 1st column is old filename and 2nd is a new filename. I want to change that list into a batch file.
Names are pretty unique so I want to make batch file - so that will be one command for every file to be renamed.
RENAME "C:\images\768e11ab.jpg" "4ca5d042.jpg"
RENAME "C:\images\5402c708.jpg" "b802820b.jpg"
RENAME "C:\images\1c039e0e.jpg" "80ce9797.jpg"
etc...
It is rather simple, only, files are scattered across subfolders. Is there any way to make a command so it will look for that specific file in all subfolders in "C:\images" and rename it.
Following some similar questions tried this, with no result:
for /r "C:\images\" "%%~G" (768e11ab.jpg) do "4ca5d042.jpg"
Also, tried to use some renaming application for this but they freeze when I try to rename big list of files, so I would avoid them and use batch file. Also, I would like to use this way where there is one line in batch file for every file because it is simpler for me to use it (and change it later). I appreciate your help.
Approach the problem from the other side. Instead of looping over the image files, loop over the text file.
Assuming your textfile to be something like
"768e11ab.jpg" "4ca5d042.jpg"
"5402c708.jpg" "b802820b.jpg"
"1c039e0e.jpg" "80ce9797.jpg"
Then the code could look like:
#echo off
REM read each line; %%A is the first column, %%B is the second one
for /f "tokens=1,2" %%A in (list.txt) do (
REM search the image file %%A
for /R "C:\Images\" %%C in ("%%~A") do (
REM %%C now holds the full path of %%A
ECHO ren "%%~C" "%%B~%%~xC"
)
)
If your list looks different, the tokens and perhaps the delims for the for /f loop have to be adapted.
NOTE: the ren command is just echoed for security reasons. Once you verified it does exactly what you want, remove the ECHO to enable the * ren` command.
I know pretty much nothing about batch programming and I would like to do the following thing.
I have a .txt file that could have one or several lines and I would like to cut the last line and insert it into another .txt file. I have been doing some research before asking this question but I couldn't find anything "understandable" to me in order to achieve this.
Also, the most appropriate QA found, was this link on Stack but I don't know how to "translate" it.
This are my lines within the .txt file
S,1,______,_,__;Cutii carton 370*290*2;1.00;1.000;1;1;1;0;0;
T,1,______,_,__;0;1.00;;;;
I would like to cut the line starting with "T" from this .txt file and insert into a new .txt file.
Could anyone help me? Thanks
EDIT
Please see the print screen
get the last (non-empty) line from a text file:
for /f "delims=" %%A in (ttt.txt) do set last=%%A
appending it to another file:
echo %last%>>ttt_lastline.txt
Using this:
#For /F %%A In ('Find /C /V ""^<"%~1"') Do #Set/A _=%%A-1
#(More +%_% "%~1")>"%~dpn1_lastline.txt"
Just drag and drop your text file onto the batch file and the last non empty line will be output into a text document in the same directory as the dropped file. The output file will have the same name as the dropped file but with _lastline appended to the end.
[Edit]If you want to hard code the file name then:
#For /F %%A In ('Find /C /V ""^<"File.txt"') Do #Set/A _=%%A-1
#(More +%_% "File.txt")>"File_lastline.txt"
You may prefer the following method although it will likely be slower especially as File.txt gets larger:
#For /F "UseBackDelims=" %%A In ("File.txt") Do #Set "last=%%A"
#(Echo %last%)>"File_lastline.txt"
To cut the first "n" lines of several files and concatenate them sequentially into one, you can use:
For %f in (list-filename*.txt) do more +n(number of lines to be removed) > type >> Outputfilename.txt %f
I have roughly 2000 documents in one folder and I want to seperate them into different folders.
I have created a "documents.txt", which lists every file I´m interested in.
The .txt file reads as:
C:\Users\NIE\Desktop\test2\one.pdf
C:\Users\NIE\Desktop\test2\two.pdf
C:\Users\NIE\Desktop\test2\three.pdf
So now I went on creating a .bat file:
#echo off
FOR /F "delims=" %a IN (C:\Users\...\Desktop\test2\documents.txt) DO COPY "%%~a" "C:\Users\...\Desktop\test2\kopieren\%%~nxa"
The folder structure is like:
In "...\Desktop\test2", all documents are located. In a subfolder ("...\Desktop\test2\copy"), the specific documents (as listed in the documents.txt) should be copied.
While running my code, I´m getting the statement:
%C:\Users\...\Desktop\test2\one.pdf
The syntax for the filename, path is wrong
0 files were copied.
So I guess the "%" seems to be the bad guy here. I tried different styles for the .txt file, like
one.pdf
userprofile%\Desktop\test2\one.pdf (thought I could use the first % for completing the "%userprofile%" stuff
Every solution I could find via google did not worked either, the formatting of the .txt file seems to be a problem in my case.
Really looking forward for you answers :)
Looks like you have done a fatal mistake: %a
Fixed:
FOR /F "delims=" %%a IN (C:\Users\...\Desktop\test2\documents.txt) DO COPY "%%~a" "C:\Users\...\Desktop\test2\kopieren\%%~nxa"
I am creating a small batch application and I would like to know how to to put file contents into variables or wildcards. (Don't know which one it is) I used to be able to do "set blablabla=< MainChat.txt" but it is not working. It would be very helpful if someone could tell me how to load one line of a file into a variable, and the next line into another.
read file line by line including empty lines -> http://www.dostips.com/forum/viewtopic.php?p=9611
-OR-
processing text file:
for /f "useback tokens=* delims=" %%# in ("c:\text.file") do (
set "current_line=%%#"
)
more info here: http://ss64.com/nt/for_f.html
I am using the following batch file to make a zip file for each xml in a folder:
FOR %%f in ("C:\files\*.xml") DO 7za.exe a C:\files\zips\%%~nf.zip (%%f)
However if the file name has a space in it (test plop.xml) then the batch file does not work. It seems to split the name and thinks it is 2 files.
How to modify the batch file so that it properly handles file names with spaces?
Try placing quotes around the output file name.
Change
FOR %%f in ("C:\files*.xml") DO 7za.exe a C:\files\zips\%%~nf.zip (%%f)
to:
FOR %%f in ("C:\files*.xml") DO 7za.exe a "C:\files\zips\%%~nf.zip" (%%f)
May also be the variable %%f, may need to place quotes around this as well.