Find the most recently created file in the directories and sub-directories using PowerShell or Windows Batch - powershell

I have 30 different folders which I need to iterate thru, within each one there’s a Log folder and inside that, are the text files. I’m after the latest one, which I need to copy to the new location with the preferred name (E.G. 2020-03-28.txt.FolderServerName1, where appended variable FolderServerName1, identifies from which server it came from)
set source="\\ServerName\LogFolders"
set target=" C:\Data\CopiedLogFiles"
FolderServerName1
Log
2020-03-26.txt
2020-03-27.txt
**2020-03-28.txt**
FolderServerName2
Log
2020-03-26.txt
2020-03-27.txt
**2020-03-28.txt**
FolderServerName3
Log
2020-03-26.txt
2020-03-27.txt
**2020-03-28.txt**
https://devblogs.microsoft.com/oldnewthing/20120801-00/?p=6993
The post above is very useful, but I think I need another nested loop within, which I'm struggling with syntactically.
Thank You so much!

This file copying task can be done with:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /D %%I in ("\\ServerName\LogFolders\*") do (
set "CopyDone="
for /F "delims=" %%J in ('dir "%%I\Log\20??-??-??.txt" /A-D-H /B /O-N 2^>nul') do if not defined CopyDone (
copy /Y "%%I\Log\%%J" "C:\Data\CopiedLogFiles\%%~nJ_%%~nxI%%~xJ" >nul
set "CopyDone=1"
)
)
endlocal
For each non-hidden subdirectory in \\ServerName\LogFolders the outer FOR loop first deletes the environment variable CopyDone and runs one more FOR loop.
The inner FOR loop starts in background one more command process using %ComSpec% /c and the command line enclosed in ' as additional commands. So executed with Windows installed into C:\Windows is in background for example:
C:\Windows\System32\cmd.exe /c dir "\\ServerName\LogFolders\FolderServerName1\Log\20??-??-??.txt" /A-D /B /O-N 2>nul
The command DIR searches in specified directory
only for non-hidden files because of option /A-D-H (attribute not directory and not hidden)
with a file name matching the wildcard pattern 20??-??-??.txt
and outputs in bare format because of option /B just each file name with extension without path
ordered reverse by name because of option /O-N
to handle STDOUT (standard output) of the background command process.
The reverse output sorted alphabetically by name results for the log file names that 2020-03-28.txt is output on first line, 2020-03-27.txt on second line and 2020-03-26.txt on third line.
It could be that the subdirectory Log does not exist at all or does not contain any file matching the wildcard pattern. In this case command DIR outputs an error message to handle STDERR (standard error) which is suppressed by redirecting it to device NUL with 2>nul.
Read the Microsoft article about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
The command process processing the batch file captures everything written to handle STDOUT of background command process and FOR processes this captured output line by line after started cmd.exe terminated itself.
FOR ignores empty lines which do not occur here.
FOR would split up by default each line into substrings using normal space and horizontal tab character as string delimiters, would next look if first substring starts with default end of line character ; in which case the line would be also ignored and otherwise would assign just first space/tab delimited string to specified loop variable. It would be possible here to use this default line processing behavior as the file names do not contain a space character. But it is nevertheless better to disable line splitting behavior by using delims= to define an empty list of string delimiters.
So the inner FOR assigns on first loop iteration the file name of newest file according to international formatted date in file name to loop variable J and runs the command IF.
On first loop iteration of the inner FOR loop the environment variable CopyDone is always not defined as made sure on the command line above and so the IF condition is true.
For that reason the file is copied which means for first folder FolderServerName1 that the executed COPY command is:
copy /Y "\\ServerName\LogFolders\FolderServerName1\Log\2020-03-28.txt" "C:\Data\CopiedLogFiles\2020-03-28_FolderServerName1.txt" >nul
The target file name is modified from requested 2020-03-28.txt.FolderServerName1 to 2020-03-28_FolderServerName1.txt. It is in general better to use a dot in the name of a file just once as separator between file name and file extension and keep the file extension at end of the name of the file to be able to open the file with a double click.
The environment variable CopyDone is defined with a value after the file copying is done without verification on success. The string value assigned to environment variable CopyDone does not matter in this case.
The inner FOR continues processing the captured lines by assigning one file name after the other to loop variable J and running the IF condition. But this condition is not true for any other file name than the first file name.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
copy /?
dir /?
echo /?
endlocal /?
for /?
if /?
set /?
setlocal /?

Related

Assist with batch file that searches and xcopy hidden file to specific dir

It's annoying to manually always search in the CMD and xcopy the hidden file, can someone whos good in scipting help me out?
I use these 2 commands:
Firstly i open CMD in the FOLDER2 and entering this command to find the hidden file in the hidden random sub dir:
dir /s /b | find "robotknow"
(robotknow is not the fullname of the file, only part of it.)
And then when it find the hidden file within the random made subdir i copy the whole path including the whole filename with the ending.
Xcopy /h *The whole path including the filename and ending* C:\hello
My folders:
$sourceDir = 'C:\Users\USER\AppData\Local\Packages\FOLDER1\FOLDER2'
$targetDir = 'C:\hello'
So i wish to create a batch that could search that string "robotknow" and copy the fullname of the file to my tagetdir.
Is it possible?
Im trying to learn commands but batching is harder, if i was unclear on anything please ask me thank you!
Edit:
I found few commands that could be useful but I dont know how to use them so that it works.
$searchStrings = For it to search after the string above i mentioned: "robotknow"
And
Copy-Item $_.FullName $targetpath
An example would be:
The filename has this in it's name "robotknow" and i want to copy it.
Copy the file im searching after to copy thats within the sub folder of the FOLDER2 which is an hidden random folder that i cannot se:
%LocalAppData%\Packages\FOLDER1\FOLDER2\THE-hidden-RANDOM-made-sub-DIR.
Copy it to it's final directory c:\hello
The final directory, simply just: c:\hello.
By hidden i mean that i cannot see in file explorer, windows GUI and neither if i put this simple command in CMD dir to show the hidden random folder where the file is located in, they are not showing.
The file only appears in CMD if i enter this command dir /s /b | find "robotknow" when im in the FOLDER2.
Only after that i can se the hidden random made dir/folder and the full hidden path to it (the file).
I suggest following batch file code for this task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFolder=%LocalAppData%\Packages\FOLDER1\FOLDER2"
set "TargetFolder=C:\hello"
for /F "delims=" %%I in ('dir "%SourceFolder%\*robotknow*" /A-D /B /S 2^>nul') do %SystemRoot%\System32\xcopy.exe "%%~dpI*" "%TargetFolder%\" /C /E /H /K /Q /R /Y >nul
endlocal
The command FOR with option /F starts a separate command process with cmd.exe /C (more precisely %ComSpec% /C) in background to run the command line:
dir "C:\Users\{username}\AppData\Local\Packages\FOLDER1\FOLDER2\*robotknow*" /A-D /B /S 2>nul
DIR outputs to handle STDOUT of background command process
just the names of all files matching the wildcard pattern *robotknow* because of option /A-D (attribute not directory)
even on file having hidden attribute set because of using option /A and not excluding attribute hidden
in bare format because of option /B
with full qualified path because of option /S
found in specified directory or any subdirectory also because of option /S.
It is possible that DIR does not find any file system entry matching these criteria in which case it outputs an error message to handle STDERR. This error message is suppressed by redirecting it to device NUL.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
FOR with option /F captures all lines output to handle STDOUT of started command process and processes them line by line after started cmd.exe terminated itself.
FOR ignores empty lines which do not occur here. FOR ignores by default also all lines starting with a semicolon because of eol=; is the default for end of line character option. But a full qualified file name consisting of full file path, file name and file extension cannot start with ; and so default end of line option can be kept in this case. FOR splits up the lines by default into substrings with using normal space and horizontal tab character as string delimiters and assigns just first space/tab separated substring to specified loop variable. This line splitting behavior is not wanted here because of file path could contain a space character. For that reason option delims= is used to define an empty list of delimiters which disables the line splitting behavior.
So FOR assigns to specified and case-sensitive interpreted loop variable I the full qualified file name found and output by DIR and runs the command XCOPY.
XCOPY is executed with source being the full qualified path of found file referenced with %%~dpI always ending with a backslash concatenated with wildcard * and destination directory being specified target folder C:\hello.
The appended backslash at end of destination directory path makes it 100% clear for XCOPY that the destination is a directory and not a file which prevents the prompt if destination means a directory or a file. \ at end makes also usage of option /I unnecessary and XCOPY creates the entire destination directory structure if necessary.
The other XCOPY options are for really copying all files including files with hidden attribute set in directory containing the file matching the wildcard pattern *robotknow* with all subdirectories including empty subdirectories to destination directory with keeping attributes including read-only attribute.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
set /?
setlocal /?
xcopy /?
See also the list of predefined Windows Environment Variables.

Find and replace text between certain characters with filename

Find and replace text between certain characters with filename, have about 100 files that require this so I'm thinking it needs a occurrence or something.
Original file name: test1.txt
Inside file replace the following two lines with the file name (inside each file the below strings might not be right after each other):
location000:/computer/[project]/name/123.php,32,1,2,512,0,,txt
newlocation000:/computer/[project]/name/123.php,32,1,2,512,0,,txt
Output in file test1.txt
location000:/computer/[project]/name/test1.php,32,1,2,512,0,,txt
newlocation000:/computer/[project]/name/test1.php,32,1,2,512,0,,txt
This is an easy to achieve task with using JREPL.BAT written by Dave Benham which is a batch file / JScript hybrid to run a regular expression replace on a file using JScript.
#echo off
if not exist "%~dp0jrepl.bat" (
echo ERROR: JREPL.BAT missing in directory "%~dp0".
echo/
pause
goto :EOF
)
for %%I in ("C:\Temp\*.txt") do call "%~dp0jrepl.bat" "^((?:new)?location000:/.+/).*(\.[^.,]+,)" "$1%%~nI$2" /F "%%I" /O -
This batch file works only on NTFS drives. It can result in an endless running loop on FAT16, FAT32 or ExFAT drives or skipping some text files. For a working solution independent on file system replace the last command line by:
for /F "eol=| delims=" %%I in ('dir "C:\Temp\*.txt" /A-D-H /B /ON 2^>nul') do call "%~dp0jrepl.bat" "^((?:new)?location000:/.+/).*(\.[^.,]+,)" "$1%%~nI$2" /F "C:\Temp\%%I" /O -
The batch file JREPL.BAT must be stored in same directory as the batch file with the code above. For that reason the batch file checks first if JREPL.BAT really exists in directory of the batch file and if this is not the case, outputs an error message, halts script execution to make it possible for a user to read that error message and then exits. See Where does GOTO :EOF return to?
The command FOR searches in specified directory C:\Temp for non hidden files matching the wildcard pattern *.txt and calls for each found text file the batch file JREPL.BAT to replace the file name between last / and first string starting with ., having one or more characters not being a dot or a comma with next character being a , (= file extension and comma) on lines starting case-sensitive with location000:/ or newlocation000:/ by the file name of the current *.txt file without file extension. So a file name in existing file can contain also one or more . in file name before file extension.
The solution working also on FAT drives uses command DIR to get a captured list of *.txt files with just file name and file extension without path and FOR processes this file names list line by line, i.e. file name by file name. So the list of *.txt files to process does not change on running FOR calling JREPL.BAT as it would be the case on using FOR directly to find the *.txt files on FAT drives.
NTFS returns a list of directory entries matching a wildcard pattern sorted alphabetically and so the list of *.txt files does not modify during FOR iterations in this case. But all FAT file systems return the list of directory entries matching a wildcard pattern according to last modification in directory with last modified file at end of the list. So while FOR gets one file name after the other on using for %%I in ("C:\Temp\*.txt") do from file system and processes the file with calling jrepl.bat which modifies the file, the file list changes on FAT drives and next directory entry returned to FOR on its search for *.txt is either the file just modified (= endless loop) or another file after skipping a file which should be also modified because of file list changed since last directory access by FOR.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains also %~dp0 ... drive and path of argument 0 being the batch file itself.
dir /?
echo /?
for /?
goto /?
if /?
pause /?
jrepl.bat /?

Why does Powershell get executed 2 or 4 times?

I am making a API for Batch. One of its functions is a colortext, which uses PowerShell (and findstr if PowerShell is not supported on the machine) to do its work. (It simply converts the batch color given, like 3C, converts it to PowerShell-friendly names, like DarkCyan and Red, and prints the text given in color using more things.)
However, when I try to set the foreground as F (White), for some reason it tries to execute it 2 or 4 times (depending if I have Delayed Expansion enabled or not).
It sometimes fails on the last run/try showing the same thing if the supposed %fcn% and %bcn% never were applied. (this also depends if I have Delayed Expansion enabled or not).
I only want this to execute one time (and not fail for whatever reason), but it's executing 2 or 4 times for some reason. How would I do/fix this program?
Here's the minimal code needed to reproduce this problem, assuming %bcn%, %fcn% and %text% are already set (let's say Cyan as %bcn% and White as %fcn%, and Should not happen as %text%):
#echo off
set bcn=Cyan
set fcn=White
set text="This should not work"
setlocal EnableDelayedExpansion
set script=write-host %text% -ForegroundColor %fcn% -BackgroundColor %bcn%
if EXIST colortest.ps1 ( del colortest.ps1 )
echo %script% >> colortest.ps1
#echo on
#powershell -executionpolicy remotesigned -file colortest.ps1
#echo off
The output of this becomes:
If I do a color that works (like CD), with the same text:
EDIT: Since no one seems to say "Hey it's working perfectly", I meant about the second bogus execution, I know this works fine, I'm just trying to get rid of the bogus second execution.
Here is the entire code of the batch file (save it as bapi.bat and try to run "bapi colortext "This should not work" BF") in a pastebin:
http://pastebin.com/WyayVUVS
The problem can't be reproduced with the posted batch code and the values for the environment variables.
However some suggestions as partly also posted by aschipfl in his comment.
1. Deletion of a single file
The deletion of a single file can be done with either
if exist colortest.ps1 del colortest.ps1
or with
del colortest.ps1 2>nul
The first command line checks first if the file to delete exists and runs command DEL only if the file really exists.
The second command line runs the command DEL and redirects an error message written to handle STDERR to device NUL to suppress it. Command DEL outputs an error message if the file to delete does not exist at all.
On running just a single command after an IF or in a FOR loop it is really not necessary to write this command line in round brackets which define a command block. FOR and IF are designed for execution of a single command and using ( ... ) which defines a command block is just an extension of Windows command processor to be able to run multiple commands where a single command is expected by design.
2. Redirection to file with overwriting existing file
It is possible here to use just > instead of >> to create or overwrite colortest.ps1. This avoids the need to separately delete the file before.
See also the Microsoft article Using command redirection operators.
3. Space character between output text and redirection operator
On the command line
echo %script% >> colortest.ps1
there is a space character between environment variable reference %script% and redirection operator >>. This space character is also output by command ECHO and therefore also written into the file as trailing space.
This does not matter here, but often trailing spaces are not wanted in produced file. One solution is removing the space character and use:
echo %script%>> colortest.ps1
But this can result in an unexpected behavior if the string of environment variable script ends with a space and a single digit number in range of 1 to 9 as this results after preprocessing this command line before execution in 1>> colortest.ps1 or 2>> colortest.ps1, ... which is a redirection of handle 1 to 9 to file colortest.ps1 instead of printing 1 to 9 to handle STDOUT and finally to the file.
The solution is writing the redirection first and next the command
>>colortest.ps1 echo %script%
or use delayed expansion which would be here even better in case of script PowerShell script line contains special characters.
echo !script!>>colortest.ps1
The space character between >> and file name colortest.ps1 is ignored by Windows command interpreter.
4. Quoting parameter strings with special characters
The environment variable text could hold a text which requires double quoting this parameter string for correct processing by Windows command interpreter and by PowerShell. A space character (delimiter on command line) and the characters &()[]{}^=;!'+,`~<>| require often enclosing the entire parameter string in double quotes for being interpreted completely as literal text.
set "script=write-host "!text!" -ForegroundColor %fcn% -BackgroundColor %bcn%"
See also answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? why set "script=script line" is used instead of just set script=script line and why it does not matter how many double quotes are specified in the script line on assigning it to the environment variable.
5. Double quotes to output in text in PowerShell script file
To output also text containing 1 or more double quotes by the PowerShell script it is necessary to escape each double quote character with one more " before writing the text string into the script file colortest.ps1.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "bcn=Cyan"
set "fcn=White"
set "text="This should not happen^^!""
set "text=!text:"=""!"
set "script=write-host "!text!" -ForegroundColor %fcn% -BackgroundColor %bcn%"
echo !script!>colortest.ps1
echo on
#powershell -executionpolicy remotesigned -file colortest.ps1
#echo off
endlocal
It can be seen here on this example that with delayed expansion already enabled on definition of text with a string containing also an exclamation mark to output as character additionally to the also to output two double quotes that the exclamation mark must be escaped with two caret characters ^^ for assigning the exclamation mark as literal character to environment variable text. That would not be necessary on definition of text before enabling delayed expansion.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "text="This should not happen!""
setlocal EnableDelayedExpansion
set "bcn=Cyan"
set "fcn=White"
set "text=!text:"=""!"
set "script=write-host "!text!" -ForegroundColor %fcn% -BackgroundColor %bcn%"
echo !script!>colortest.ps1
endlocal
echo on
#powershell -executionpolicy remotesigned -file colortest.ps1
#echo off
endlocal
For details about the commands SETLOCAL and ENDLOCAL see this answer which explains in detail how these two commands work.
This is because, for some random reason which didn't happen to the rest, the call command executed 2 times, the second without setting any %fcn% and %bcn% parameters, when something like call :label was used.
However, this does not happen with goto label.

how to rename files using command line

I want to rename 2 files "Data1.txt" to "Log1.rtf" and "Data2.txt" to "Log2.rtf"
I am currently using this code
#ECHO OFF
FOR %%I IN ("%~dp0*.txt") DO (
SET "ext=%%~xI"
SETLOCAL EnableDelayedExpansion
RENAME "%%I" "%%~nI!Log.rtf"
ENDLOCAL
)
But it give output "data1log.rtf" and "data2log.rtf"
of course:
RENAME "%%I" "%%~nI!Log.rtf"
But it give output data1log.rtf and data2log.rtf
btw. what do you try to achive with setlocal delayedexpansion and that single ! ?
EDIT: if you insist in doing it with for (because perhaps, you have many files to rename):
#echo off
setlocal enabledelayedexpansion
for %%i in (*.txt) do (
set file=%%i
set file=!file:Data=Log!
set file=!file:.txt=.rtf!
echo ren %%i !file!
)
the first set sets the variable file to the filename
the second one replaces Data with Log
the third one replaces .txt with .rtf
then rename original filename (%%i) to the changed filename (!file!)
(the echo is there to just show the command on screen instead of really renaming files. Good for testing. If you are sure, that the code does, what you want, just remove the echo)
the setlocal enabledelayedexpansion is needed to use a variable, that is changed inside a block (between ( and )) inside the same block. Therefore you have to use the variable as !var! instead of %var%
Note: of course this code could be improved, but as a beginner: keep it as simple (and readable) as possible. For example, this code will replace Data, but not data or DATA, it will not handle spaces in filenames, ...
It might work better if you used separate code to rename each of the files, or does that defeat the object?
this website shows how to rename a file using command line:
http://www.sevenforums.com/tutorials/56013-file-folder-rename-command-prompt.html
for %%a in ("%~dp0data*.txt"
) do for /f "delims=DdAaTt" %%b in ("%%~na"
) do echo ren "%%~fa" "log%%b.rtf"
It just iterates over the indicated list of files, and for each filename, uses the unnecesary characters as delimiters for an aditional for loop. That way only the number is left and used to rename the file.
Commands to rename are echoed to console. If output is correct, remove the echo command to rename the files.
User mv command to rename files in windows (Using CLI)
As mentioned in above answer, mv command can be used in windows as well to rename the filename. I tried rename and ren commands s suggested, but I was getting error: bash: ren: command not found.
Use below to change the filename:
mv filename new_filename

count number of times a string appears in a text file using script

I need to write a .bat or .cmd script that will find all instances of file type .log in the directory it is run from, and for each of those search it for "searchstring", counting how many times it appears. Then I need to rename the file (original name: "[name].log") to "name.log". This is to enable me to get a very quick visual count of the number of errors in a file (which is part of what the log contains).
I've already got the for loop that locates all *.log files, but how do I count instances of a particular string?
try this:
for /f "tokens=2delims=:" %a in ('find /c "string" *.log') do #set /a count+=%a
echo %count%
Code is for shell prompt. For shell file replace %a with %%a.