I want to get the last modified date of every file in a directory in windows, output that to a text file, and then read each line of that text file as input.
Below are the contents of my .bat file.
#echo off
WMIC DATAFILE WHERE "PATH='\\Users\\n63206\\Desktop\\Project\\'" GET Name, LastModified > out.txt
for /f %%a in (out.txt) do (
echo %%a
)
echo test
After I run the script, only "test" gets printed out. When I open out.txt, I see the last modified dates that I output to it displayed fine, and if I create a new text file (cannot use out.txt) and manually enter some lines in there, my for loop can print out all the lines in that file. Any advice to help me figure out the issue here is greatly appreciated!
The problem is that the generated file is a UTF-16, little endian, BOM prefixed file. That is, it contains two bytes per character and is prefixed by two characters, 0xFF 0xFE.
for /f is not able to read this file.
Your options are:
1 - Read the information as it is generated
for /f "delims=" %%a in (
' wmic datafile where "path='\\Users\\n63206\\Desktop\\Project\\'" get name^,lastmodified '
) do echo %%a
2 - Once the file is generated, convert the file while reading it
wmic datafile where "path='\\Users\\n63206\\Desktop\\Project\\'" get name,lastmodified > out.txt
for /f "delims=" %%a in ('type out.txt') do echo %%a
3 - Convert the file while generating it
wmic datafile where "path='\\Users\\n63206\\Desktop\\Project\\'" get name,lastmodified | find /v "" > out.txt
for /f "delims=" %%a in (out.txt) do echo %%a
Related
I have a batch script that generates a file with the following structure (example)
123|etc|etc
345|etc|etc
678|etc|etc
I want my script to print out what it finds in each line, until it finds a |, so in this example i would want it to print:
123
345
678, and so on.
I've tried findstr /V with the | but it completely ignores the line (as the command states it will)
Any idea how could i turn this around?
#echo off
for /f "usebackq tokens=1 delims=|" %%a in ("test.txt") do (
echo %%a )
pause
This is looping through a file (test.txt) in the same directory as the batch script. I tell it to only use 1 token with a pipe delimiter then we just print out %%a which has the value of your first column of data.
I think I am about to have an a brain aneurysm. I am trying to print the lines of a text file to a new file, but it only outputs the last line. I have done this a dozen times, now I am not sure what I am doing wrong.
for /F "tokens=*" %%A in (results.txt) do (
echo %%A
) > imsofrustrated.txt
PAUSE
Am I literally retarded? This is in reference to a previous question I posted.
FINDSTR - Stop on last string match
> is to redirect output and overwrite whatever is in the file.
>> is to redirect output and append to the file.
for /F "tokens=*" %%A in (results.txt) do (
echo %%A
) >> imsofrustrated.txt
I need to pull information out of a text file which is always after the third comma but before the 4th eg I need tht,1,2,THIS INFO,444
then save this into a file .txt
is this possible using a .bat
I need to pull information out of a text file from between the 3rd and 4th ,
tht,1,2,THIS INFO,444
Use the following batch file (test.cmd):
#echo off
setlocal
for /f "tokens=4 delims=," %%i in (myfile.txt) do echo %%i > newfile.txt
endlocal
Notes:
Input file is myfile.txt
Output file is newfile.txt
Example output:
F:\test>type myfile.txt
tht,1,2,THIS INFO,444
F:\test>test
F:\test>type newfile.txt
THIS INFO
F:\test>
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
for /f - Loop command against the results of another command.
try
for /f "delims=, tokens=3" %i in (myfile.txt) do echo %i
Thanks for the correction, (in comments below.)
I am relearning DOS/batch & seems different.
Script should create a text file (it does, verified) then read it line by line looking for a beginning word and discarding lines thru that line as it builds TMP.txt.
It should then create a file of remaining lines up to and including a finishing word and ignore the remainder.
Then it will look for a line containing a specific word.
Problem: I get ("inFile.txt") was unexpected. The reams of text I have pulled down into my reference directory has not been helpful as it appears (does't it always) correct from what I expect.
Here is the script to and a bit after that point:
REM Append a final line to input text
echo IDSS >> inFile.txt
REM Bookend the node's text with BEGwd and FINwd.
REM Read inFile.txt file- Ignore %%A if BEGwd not found,
REM else write remaining lines to TMP.
for /F "tokens=*" %%A ("inFile.txt") DO ( <<---- Problem line
if "*%BEGwd%*" == "*%%A*" echo "%%A" >> TMP.txt
)
REM Read TMP look for FINwd- Write non-%%B lines to TMP2
REM until FINwd
for /F "tokens=*" %%B ("TMP.txt") DO (
if "*%FINwd%*" == "*%%B*" goto SEARCH
echo "%%B" >> TMP2.txt
)
Am also unsure if batch will accept my IF command compare with *'s?
Appreciating any assistance there too.
for /F "tokens=*" %%A ("inFile.txt") DO ( <<---- Problem line
Correct syntax is for /F "USEBACKQtokens=*" %%AIN("inFile.txt") DO (
the usebackq option is required because you have "quoted your filename".
I a new to windows command line scripts.
I have a batch file which i use to merge multiple text files into one. However i want to be able to also add the name of the text file the row comes from to the end of each row in the merged file.
This is the script i am currently working with:
#ECHO OFF
ECHO Creating %1...
FOR /F "usebackq tokens=1" %%G IN (`DIR /B "C:\My Documents\Data\*.txt"`) DO
(
ECHO Adding %%G
ECHO. >> Output.txt
TYPE %%G >> Output.txt
)
Now i know that to get the filename into the output file i need to use:
ECHO %%G >> Output.txt
However i'm not sure how i would add this to the current script so it adds the filename to each row and I have had no luck with finding any examples.
There is a simple two liner that works from the command line if you are willing to prefix each line with the file name instead of putting the file name at the end. This solution is extremely fast.
cd "C:\My Documents\Data"
findstr "^" *.txt >output.log
Each line will have the format of filename:line content
It is important that your output file use a different extension than the files you are merging. Otherwise you run the risk of the command processing its own output!
The other option is to make sure the output goes to a different folder, perhaps the parent folder:
cd "C:\My Documents\Data"
findstr "^" *.txt >..\output.txt
Or, if you are willing to include the full path to each file in your output, then make sure current directory is not the same as your source, and use
findstr "^" "C:\My Documents\Data\*.txt" >output.txt
The only drawback to the above solution is that problems can arise if the last line of a text file does not end with a newline character. This will cause the first line of the next file to be appended to the last line of the prior file. Something like: FILENAME1:last line without newlineFILENAME2:first line of next file
A simple script can append a newline to files that are missing the newline on the last line. Then the files can be safely merged with the filename prefix on each line:
#echo off
if "%~1" equ ":FindFiles" goto :FindFiles
cd "C:\My Documents\Data"
:: Append newline to text files that are missing newline on last line
for /f "eol=: delims=" %%F in ('"%~f0" :FindFiles') do echo(>>"%%F"
:: Merge the text files and prefix each line with file name
findstr "^" *.txt >output.log
exit /b
:FindFiles
setlocal enableDelayedExpansion
:: Define LF to contain a newline character
set lf=^
:: The above 2 blank lines are critical - do not remove
:: List files that are missing newline on last line
findstr /vm "!lf!" *.txt
You'll need to add each line in the file individually:
#ECHO OFF
ECHO Creating %1...
SET "sourcedir=c:\sourcedir"
FOR /F "delims=" %%G IN ('DIR /B /a-d "%sourcedir%\*.txt"') DO (
ECHO Adding %%G
ECHO. >> Output.txt
for /f "usebackq tokens=*" %%a in ("%sourcedir%\%%~G") do (
Echo %%a - %%G >> Output.txt
)
)
Note in the second last line, the file name and the line is seperated by a -, you can replace this with whatever (don't forget to check for escaping characters) or can get rid of this if you want.
I'm sure that will work, but if it doesn't, tell me the Error message and I can fix it for you.
Mona
---- [edit:pw]
Close - major problem was the ( on the FOR ... %%G line was on the line following the DO - must be on the same line as the DO.
Added /a-d to the DIR to prevent subdirectory names matching
changed "usebackq tokens=1" to use conventional quotes and allow spaces in filenames
assigned target directory name to sourcedir variable and included %sourcedir% in both FOR statements to allow execution from anywhere, otherwise the filenames found in C:\My Doc.... would be searched-for in the current directory for replication into the output.
OP needs to change value assigned to sourcedir to C:\My
Documents\Data