How to grab the filestamp in a nice format? - batch-file

gurus in the Net.
I am having issues trying to output a nice looking format of this batch file :
#echo off
setLocal EnableDelayedExpansion
for /f "tokens=*" %%G in (c:\temp\machine_list.txt) do (
echo %%G
echo %%G >> c:\temp\Log.txt
dir "\\%%G\C$\Logs\filelog.txt" /A >> c:\temp\Log.txt
)
This batch works, but it is just fast and dirty:
For each machine a lot of useless information are printed :
Machine_id
Volume in drive \\Machine_001\C$\Logs is System
Volume Serial Number is 0271-2533
Directory of \\Machine_001\C$\Logs
14.03.2018 11:33 7'722 filelog.txt
1 File(s) 7'722 bytes
0 Dir(s) 347'223'621'632 bytes free
My question is :
there is a way to have a nice log ?
.i.e :
Machine_id1 14.03.2018 11:33 7'722 filelog.txt
Machine_id2 14.03.2018 11:33 7'722 filelog.txt
Machine_id3 14.03.2018 11:33 7'722 filelog.txt
thanks in advance

The following batch script should work as you intended it to:
#(For /F "UseBackQ Delims=" %%A In ("C:\temp\machine_list.txt"
) Do #For /F EOL^=^ ^ Delims^= %%B In ('Dir /A "\\%%A\C$\Logs\filelog.txt"'
) Do #Echo %%A %%B)>"C:\temp\Log.txt"
I won't explain how it all works, but the important part is the nested For loop. It runs the Dir command as intended, but ignores all lines beginning with the character SPACE. This is done by setting the end of line, EOL to that character, using a rather odd looking but effective syntax.
Edit
Perhaps this will achieve the aims of your latest comments:
#Echo Off
For /F "UseBackQ Delims=" %%A In ("C:\temp\machine_list.txt") Do (
If Exist "\\%%A\C$\Logs\" (
For /F EOL^=^ ^ Delims^= %%B In ('Dir /A "\\%%A\C$\Logs\filelog.txt"') Do >>"C:\temp\Log.txt" Echo %%A %%B
) Else >>"C:\temp\Log_error.txt" Echo %%A)
Edit 2
This is a futher modified example for hopefully better logging of errors:
#Echo Off
For /F "UseBackQ Delims=" %%A In ("C:\temp\machine_list.txt") Do (
If Exist "\\%%A\C$\Logs\filelog.txt" (
(For /F EOL^=^ ^ Delims^= %%B In (
'Dir /A "\\%%A\C$\Logs\filelog.txt"'
) Do Echo %%A %%B)>>"C:\temp\Log.txt") Else ((
If Not Exist "\\%%A\C$\" Echo %%A inaccessible
Echo %%A missing filelog.txt)>>"C:\temp\ErrLog.txt"))

Related

Specific use of double loop

I recently asked you guys some help for a double conditions in batch file. You guys helped me very well but I'm now struggling with a new trouble... I'm trying to optimize my script.
Previous question (How to double conditions in batch file?)
I would like to list all files with the .doc extension that are in the subfolders named on the current date only. I think I'm making a mistake on the use of this double loop.
#echo off
chcp 1252
set Pathname="D:\testDir"
set year=%date:~-4%
set month=%date:~-7,2%
set day=%date:~-10,2%
set logfile=%Pathname%\logs\log.txt
cd %Pathname%
d:
for /D /R %%i in (*%year%_%month%_%day%*) do (
for /R %%i %%s in (*.doc) do (
echo "file : %%s worked and does an output at %time%" >> %logfile%
)
)
Can you give me a little help? Thanks for your help and advices.
EDIT: I need to perform actions on each .doc file in folders containing the string YEAR_MONTH_DAY. But the .doc files can also be located in other subdirectories.
It could be something like that :
D:\testDir\directory1_2021_11_16\test.doc
D:\testDir\directory1_2021_11_16\test.log
D:\testDir\directory1_2021_11_16\subDirectory1\test.doc
D:\testDir\directory1_2021_11_16\subDirectory1\test.log
D:\testDir\directory1_2021_11_17\test.doc
D:\testDir\directory1_2021_11_17\test.log
D:\testDir\directory1_2021_11_17\subDirectory1\test.doc
D:\testDir\directory1_2021_11_17\subDirectory1\test.log
D:\testDir\directory1_2021_11_17\subDirectory2\test.doc
D:\testDir\directory1_2021_11_17\subDirectory2\test.log
D:\testDir\directory1_2021_11_17\subDirectory2\subSubDirectory1\test.doc
D:\testDir\directory1_2021_11_17\subDirectory2\ubSubDirectory1\test.log
D:\testDir\directoryThatIDontCare\test.doc
D:\testDir\directoryThatIDontCare\test.log
D:\testDir\directoryThatIDontCare\subDirectory1\test.doc
D:\testDir\directoryThatIDontCare\subDirectory1\test.log
Any solutions ? Thanks for your time guys !
Here's an example script you can learn from, and run, to achieve what your submitted code was intending to do.
#Echo Off
SetLocal EnableExtensions
Set "CurDate="
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
For /F "Delims=" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Path
Win32_LocalTime Get Day^, Month^, Year /Format:List
2^>NUL') Do For /F "Tokens=*" %%H In ("%%G") Do Set /A "_%%G + 10000"
For /F "Tokens=1,* Delims==" %%G In ('"(Set _) 2>NUL"') Do (
SetLocal EnableDelayedExpansion & If %%H Gtr 10059 (For %%I In (!%%G:~-4!
) Do EndLocal & Set "%%G=%%I") Else For %%I In (!%%G:~-2!
) Do EndLocal & Set "%%G=%%I")
Set CurDate=%_Year%_%_Month%_%_Day%
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
If Not Defined CurDate GoTo :EOF
Set "BaseLocation=D:\testDir"
PushD "%BaseLocation%" 2>NUL || GoTo :EOF
Dir "logs" /B /A:D 1>NUL 2>&1 || (MD "logs\%CurDate%" 2>NUL || GoTo :EOF)
Set "LogFile=%BaseLocation%\logs\log.txt"
For /F "EOL=? Delims=" %%G In ('Dir "*_%CurDate%" /B /A:D 2^>NUL'
) Do For /F Delims^= %%H In ('Set "PATHEXT^=" ^& %SystemRoot%\System32\where.exe
/F /R "%%G" "test.doc" 2^>NUL'
) Do (Echo file : %%~H worked and does an output at %TIME%) 1>>"%LogFile%"
If you want to know how any of it works, please use the built-in help information for each command, use the site search facility and/or your chosen search provider. I am not a private tutor, so will not be performing such a role.

How to insert a revision number in string

I’m using the following script to add a revision number to a .stp file. For example: ABS0012033.stp to ABS0012033_rev001.stp. This is working well.
Now I have some files that end with _asm.stp, for example, ABS0012033_asm.stp. How do I need to modify the script that it works for both file types?
Some additional information: There is only one .stp file to be renamed per time and then the .stp file will be moved to another folder. The text file which contains the revision number is temporary and will be deleted after renaming the .stp file.
The current script does remove the last 4 characters from the filename and stores the filename in variable str1. Then it renames the filename with str1 + revision number.
for /F "usebackq tokens=2" %%a IN (`findstr REVISION C:\PUBLISH_WORKSPACE\*.txt`) do (
SET Rev=%%a)
FOR %%S IN ( c:\publish_workspace\*.STP) DO (SET FILE=%%S)
set str1=%file:~21,-4%
ren %file% %str1%_rev%REV%.stp
EXIT
I have tried to implement an IF function in the following script, but it doesn’t work. It doesn’t write str1 when renaming the filename. Any ideas what the problem could be?
for /F "usebackq tokens=2" %%a IN (`findstr REVISION C:\PUBLISH_WORKSPACE\*.txt`) do (
SET Rev=%%a)
FOR %%S IN ( c:\publish_workspace\*.STP) DO (SET FILE=%%S)
echo.%FILE%|findstr /C:"_asm" >nul 2>&1
if not errorlevel 1 (
set str1=%file:~21,-8%
ren %file% %str1%_rev%REV%.stp
) else (
set str1=%file:~21,-4%
ren %file% %str1%_rex%REV%.stp
)
pause
move c:\publish_workspace\*.stp c:\publish_workspace\stp
del c:\publish_workspace\*.txt
EXIT
Sorry, I'm not sure to understand the problem. What about this?
set Rev=ren001
ren ABS???????_asm.stp ABS???????_%Rev%.stp
This answer makes assumptions, those assumptions could have been resolved had you responded to my comment one day ago.
#Echo Off & SetLocal EnableExtensions
PushD "C:\Publish_Workspace" 2>NUL && (Set "Rev=") || GoTo :EOF
For /F "Tokens=1,* Delims=:" %%G In ('%SystemRoot%\System32\findstr.exe "REVISION" "*.txt"') Do Set "Rev=%%H"
If Not Defined Rev GoTo :EOF
For /F Delims^=^ EOL^= %%G In ('Set "PathExt=" ^& %SystemRoot%\System32\where.exe ".":"*.stp" 2^>NUL') Do For /F Delims^=_^ EOL^= %%H In ("%%~nG") Do Ren "%%G" "%%H_rev%Rev:* =%%%~xG"
I am not a tutor, and will not be providing additional explanation, other than to refer you to my comments, and the built-in help information for each command, (commandName /?).

Batch script to read a specific line from a log file

I am trying to write a batch script that reads 18th line from a .log file and outputs the same. The .log file name varies each time. abc_XXXX.log where xxxx are process IDs. Below is the code I am trying to run to achieve this.
:Test1
set "xprvar=" for /F "skip=17 delims=" %%p in (abc*.log) do (echo %%p& goto
break)
:break
pause
goto END
set var=anyCommand doesn't work. It just sets the var to the literal string.
The usage of afor /f is the right way, just the variable assignment works different:
for /F "skip=17 delims=" %%p in ('dir /b abc*.log') do ( set "xprvar=%%p"& goto break )
There is also an option using FindStr
#Echo Off
For /F "Tokens=1-2* Delims=:" %%A In ('FindStr/N "^" "abc_*.log" 2^>Nul'
) Do If %%B Equ 18 Echo %%A:%%C
Pause
The above example Echoes the <filename>:<18th line content>, but there's no reason in the appropriate situation why you couldn't change that to read:
#Echo Off
For /F "Tokens=1-2* Delims=:" %%A In ('FindStr/N "^" "abc_*.log" 2^>Nul'
) Do If %%B Equ 18 Set "xprvar=%%C"
If there is more than one matching filename in the directory, the variable would be set to the content in the last file parsed.
#ECHO Off
SETLOCAL
FOR %%f IN (abc*.log) DO (
SET "reported="
FOR /f "skip=17delims=" %%p IN (%%f) DO IF NOT DEFINED reported (
ECHO %%p
SET "reported=Y"
)
)
Assign each filename in turn to %%f.
For each filename found, clear the reported flag then read the file, skipping the first 17 lines. echo the 18th line and set the reported flag so that the remainder of the lines are not echoed.

batch script to read a file containing path-to-dir, and get the latest file,read the last line from the file and print

Apologies for the very long title, i was wondering if some one could help me out with this.
What I'm trying to do is read a file which contains, say, 10 different directory paths, get the latest file in that directory and read the last line from that file.
#echo off
setlocal enableextensions enabledelayedexpansion
set host=%COMPUTERNAME%
echo Host: %host%
for /f "tokens=* delims=" %%I in (C:\temp\servers.txt) do (
SET /A vidx=!vidx! + 1
set var!vidx!=%%I
echo Path-to-File: %%I
for /f "tokens=* delims=" %%X in ('dir "%%I" /OD /B') do (
set newest=%%X
)
echo %newest%
)
Here is the output:
Host: Windows7
Path-to-File: \\Windows7\C$\direct\log\direct
ECHO is off.
Path-to-File: \\Windows7\C$\temp
ECHO is off.
Thanks in advance for the help.
Excuse me, I am somewhat confused by your description. The Batch file below was written in accordance with I understood about your requirement.
#echo off
setlocal EnableDelayedExpansion
echo Host: %COMPUTERNAME%
rem Process the file with the paths:
for /F "delims=" %%I in (C:\temp\servers.txt) do (
echo Path-to-File: %%I
rem Get the newest file in this directory
for /F "delims=" %%X in ('dir "%%I" /OD /B') do (
set "newest=%%~X"
)
echo Newest-File: !newest!
rem Read the last line from this file
for /F "usebackq delims=" %%L in ("!newest!") do (
set "lastLine=%%L"
)
echo Last-Line: !lastLine!
)
I hope it helps...
Antonio

How do I only assign FOR /F token from one line only in a test file?

How do I make this grab the token from the first line ONLY in .txt file instead of looping through every line. I want %%m to be assigned to the 3rd token on line one only then stop.
#echo off
FOR %%A IN (.\xdrive\*.txt) DO (
FOR /F "usebackq tokens=3 delims=," %%m IN ("%%A") DO (
IF "%%m" == "F01" (xcopy /Y "%%A" .\Outbound)
pause
)
)
pause
set /p can be used to read the first line, and then you can use a FOR /F loop to get the third token
setlocal EnableDelayedExpansion
FOR %%A IN (%1) DO (
<%%A set /p firstline=
FOR /F "tokens=3 delims=," %%m IN ("!firstline!") DO (
echo %%m
)
)
Without see the eg files and knowing exactly what you're trying to do I can't test this, but here's the listing of firstline.bat which should do what you're asking for :) At first I thought this needed to be more complicated than it is... after your first if simply use a goto to exit the for structure after it's first call - problem solved?
#echo off
::: firstline.bat - Retrieve the first line from a series of files
::: usage: firstline $filespec
::: filespace - files to process (eg .\xdrive\*.txt)
if "%~1"=="" findstr "^:::" "%~f0"&GOTO:EOF
FOR %%A IN (%1) DO (
call :testfirst "%%A"
)
goto :eof
:testfirst
FOR /F "usebackq tokens=3 delims=," %%m IN (%1) DO (
IF "%%m" == "F01" (xcopy /Y %1 .\Outbound)
goto:eof
)
See this post, which shows how to mimic the gnu head utility using a dos batch file:
Windows batch command(s) to read first line from text file
untested
read first line tokens3
for /f "tokens=3 delims=," %%a in ('"findstr /n . %1|findstr /b 1:"') do set fltok3=%%a
echo(%fltok3%
Hackish =
#echo off
FOR %%A IN (.\xdrive\*.txt) DO (
FOR /F "usebackq tokens=3 delims=," %%m IN ("%%A") DO (
IF "%%m" == "F01" (xcopy /Y "%%A" .\Outbound)
GOTO:EOF
)
)
So all you're doing is escaping the loop after the first pass instead of continuing onto the next line.

Resources