how to find the last time stamp of the particular TYPE file in that directory? - command-line

how get the date and time of the last modified particular TYPE file in that directory
let me explain with an example
if i use the command dir *.reo /o:d
i get the all *.reo files in that directory sorted according to the date ..
this is the the last line of the output
29-03-2010 11.31 arun.reo
now i just want to copy the date and time of the last created file in variable or file .is it possible ?

You can do this using a batch file like this:
#echo off
setlocal enableextensions
for /f "delims=" %%f in ('dir *.reo /o:-d /b') do (
set dt=%%~tf
goto endloop
)
: endloop
echo %dt%
A little explanation:
dir *.reo /o:-d /b produces a list of all .reo files in the current directory, sorted by date in descending order (so that the last modified file comes first).
%%~tf expands to the date of the file specified by the %f variable.

It depends on what os your using. If you want just the last file
command one pipes the complete directory into a temp file
dir *.reo /o:d > temp.txt;
command two gets the last line of the temp file. Only works if you have the windowserver 2003 install which the link is provided below.
tail - 1 temp.txt;
Go to the Microsoft Windows Server 2003 download section at http://www.microsoft.com/windowsserver2003/
downloads/tools/default.mspx. Or, if that link does not work, visit http://www.microsoft.com/ and search for "Windows 2003". Once there, choose the "Downloads -> Tools" link.

Related

Batch Script that gives unique time stamp to rename bulk files

I used this script to rename files in bulk with a unique time stamp in a batch file. The idea is to use the second and millisecond to create a unique number, which is added to the filename. The why? The filenames being used is so similar in the naming convention that it is triggering errors on the import program into the database. What doesn't trigger the error is the uniqueness of the filename, which is why I thought of a second and millisecond stamp to the prefix. Now, the code works as intended, but the stamp is not unique. It is as if the code pulls the value from the system and applies to all of the files in the folder instead of one file at a time.
Assume my file names are like this:
File1.txt
File2.txt
File3.txt
File4.txt
When the script is run, it renames the files like this:
1410File1.txt
1410File2.txt
1410File3.txt
1410File4.txt
I was hoping the script would do it like this:
1410File1.txt
1411File2.txt
1412File3.txt
1413File4.txt
The code I used to help me with this problem:
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set Mil=%dt:~14,6%
set stamp=%Sec%%Mil%
CHDIR /D "c:\TEST\FILES\"
for %%a in (*.*) do ren "%%a" "%stamp%%%a"
Is there an additional programming code that is needed to ensure each filename is renamed differently? I'm on Windows 10 using a simple .bat file. On any given day, I would end up renaming anywhere from 600 up to 1000 files in one sitting so any help would be greatly appreciated!
Thanks in advance!
You answered your own question:
Now, the code works as intended, but the stamp is not unique. It is as if the code pulls the value from the system and applies to all of the files in the folder instead of one file at a time.
Take a look at your code. You
Compute the timestamp once at the start of the run:
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set Mil=%dt:~14,6%
set stamp=%Sec%%Mil%
cd down to the desired directory:
CHDIR /D "c:\TEST\FILES\"
iterate over the files in the directory, adding the same timestamp to each file:
for %%a in (*.*) do ren "%%a" "%stamp%%%a"
You need to recompute the timestamp on every iteration of the final loop.
I would also add a pause of a few milliseconds in the loop to ensure that at least 1 millisecond has elapsed between computations of the timestamp.

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 /?

Unzip all files in Directory and Subdirectory with specific file name windows 2012 R2

I am new to powershell and batch scripting.
I have a dir structure as
Z:\abc\data1\dump data1 (2018 01)\dumpdata1_1705_as_tap.zip
Z:\abc\data2\dump data2 (2018 01)\dumpdata2_1801_as_tap.zip
Z:\abc\data3\dump data3 (2018 01)\dumpdata3_1802_as_tap.zip
Z:\abc\data4\dump data4 (2018 01)\dumpdata4_1803_as_tap.zip
I want to unzip all the 1000+ files all having common name dumpdata_yymm_as_tap.zip as mentioned above. I want to unzip all of them to an different directory eg.
f:\data1\dumpdata1_1705_as_tap.zip
f:\data1\dumpdata2_1801_as_tap.zip
while preserving it name as it is and I want them after an specific date say (2017 05) in the sub_directory folder.
How can we add regex in the batch file or powershell to only extract the data after (2017 05) to (2018 04).
I have 7-zip installed but cannot install python on it.
We normally do not give out code unless the user has at least attempted to write some code of their own. You know the whole, give a man a fish or teach a man to fish philosophy.
#echo off
setlocal EnableDelayedExpansion
REM Find all zipped dumpdata files
FOR /R Z:\abc %%G IN (dumpdata*_*_as_tap.zip) DO (
REM extract date from folder name date is surrounded by parentheses.
FOR /F "TOKENS=2 delims=()" %%H IN ("%~pG") DO (
SET "fdate=%%~H"
REM remove space from date
set "fdate=!fdate: =!
REM check if date is between two dates
IF !fdate! GTR 201705 IF !fdate! LEQ 201804 (
REM 7zip command here. %%G is your zip file name
)
)
)

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.

Batch file for loop to unzip files and get timestamps of zip file contents

I have a daily process which generates some zip files out of files that are being created by other processes. I need to create a daily log file which indicates the timestamps the contents of one specific file of each zip file that is found.
I created the following batch script which seemed to work yesterday on my test system, but not anymore today, no idea why...
set VersionDirectory=C:\Test\VersionX\
set ResultOutputFile=C:\Test\LogFile.txt
for /f %%f in ('dir /b %VersionDirectory%\Installable\Packages\pattern*.zip') do (
mkdir %temp%\%%f\
unzip -extract -dir %VersionDirectory%\Installable\Packages\%%f %temp%\%%f\ > nul
for %%a in (%temp%\%%f\InstallScript.xml) do set InstallScriptXMLDate=%%~ta
rmdir /s /q %temp%\%%f\
echo %%f [package from %InstallScriptXMLDate%] >> %ResultOutputFile%
)
Short summary of what this file is supposed to do:
Loop through each pattern*.zip file in C:\Test\VersionX\ directory
unzip this file to the %temp%\%%f directory (where %%f is the filename)
Get the timestamp of the %temp%\%%f\InstallScript.xml and put it in the %InstallScriptXMLDate% variable
Delete the %temp%\%%f directory
Echo the filename (%%f) and timestamp (%InstallScriptXMLDate%) into the log file
As of now the log file just contains the filenames, followed by the string '[package from ]' string, but missing the actual date timestamp
The unzipping and removing of the zip files is working flawlessly, it's just the timestamp that's not being set.
You are setting a variable and using it in the same block. This cannot work in cmd because environment variables are expanded when a statement is parsed not when it's executed. So when the loop is run all variables have already been replaced with the values they had before the loop.
Put
setlocal enabledelayedexpansion
at the start of your batch and use !InstallScriptXmlDate! instead of %InstallScriptXmlDate%.
Another note: for is perfectly capable of iterating over files by itself, you almost never need to iterate over dir /b output with for /f. In fact, it can introduce problems that can be avoided with
for %%f in (%VersionDirectory%\Installable\Packages\pattern*.zip)