Batch file that will modify date in txt file - batch-file

I have a line in a text file that I'm trying to modify using a batch file. The line in this case is the lastexportdate=2014-01-01. I'm trying to get the batch file to modify this from
lastexportdate=2014-01-01
to
lastexportdate= Current timestamp.
Is this possible to have this equal the timestamp of my computer? Not certain how to go about tackling this, so if anyone could help that would be awesome. I'm using Windows 7.
File contents below:
root.footer=</CREDITEXPORT>
root.header=<CREDITEXPORT>
emailaddronsuccess=test#test.com
emailaddronerror=test#test.com
rerunexportdate=2014-09-06
errorfilelocation=C:\\
lastexportdate=2014-01-01
xmloutputlocation=C:\\

#echo off
setlocal
rem getting current time stamp
set "beginJS=mshta "javascript:var t=new Date();var dd=t.getDate();var mm=t.getMonth()+1;var yyyy=t.getFullYear();if(dd^<10) dd='0'+dd;if(mm^<10) mm='0'+mm;close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write("
set "endJS=));""
:: FOR /F does not need pipe
for /f %%N in (
'%beginJS% yyyy+'-'+mm+'-'+dd %endJS%'
) do set cds=%%N
echo cds=%cds%
rem processing the file
rem set the location of the file
set "content_file=c:\some.file"
break>temp.file
for /f "usebackq tokens=1* delims==" %%a in ("%content_file%") do (
if "%%a" NEQ "lastexportdate" echo %%a=%%b>>temp.file
if "%%a" EQU "lastexportdate" echo %%a=%cds%>>temp.file
)
Not tested.Just change the location of the content_file and check if the created temp.file is ok.
To replace the file add move temp.file "%content_file%" at the end.

Related

Creating a batch file to prompt for response and replace lines of text in a text or .ini file

I have gotten the batch file to work for the first line, however when I try to add an additional FOR loop to get the next line of text it actually copies or duplicates the text instead of replacing it.
currently the batch file will not even change the .txt file.
Code below --
::Find and Replace script allows the user to
::define a file path, file name and a string
::to find and replace so as to create a new file.
::
::Original file is backed up with an added extension of *.bak, in case
::the user finds the need to go back to the original.
#echo off
::Use the path from whence the script was executed as
::the Current Working Directory
set CWD=%~dp0
::***BEGIN MODIFY BLOCK***
::The variables below should be modified to the
::files to be changed and the strings to find/replace
::Include trailing backslash in _FilePath
set _FilePath=C:\
set _FileName=Password1.txt
::_WrkFile is the file on which the script will make
::modifications.
set /P Password="What is the admin Password?"
set /P AuditPassword="What is the Audit Password?"
set oldPass="cmspassword=Password1"
set OldAudit="existingauditingdbpassword=Password1"
set CMSAdmin="cmspassword="
set CMSAudit="existingauditingdbpassword="
set OldStr=%oldPass%
set OldStr2=%OldAudit%
set NewStr="%CMSAdmin%%Password%"
set NewStr2="%CMSAudit%%AuditPassword%"
::***END MODIFY BLOCK***
::Set a variable which is used by the
::search and replace section to let us
::know if the string to be modified was
::found or not.
set _Found=Not found
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
if not exist "%_FilePath%%_FileName%" goto :NotFound
echo Searching for %OldStr% string...
echo.
for /f "usebackq tokens=*" %%a in ("%_FilePath%%_FileName%") do (
set _LineChk=%%a
if "!_LineChk!"==%OldStr% (
SET _Found=Found
SET NewStr=!NewStr:^"=!
echo !NewStr!
) else (echo %%a)
)>>"%_FilePath%%_FileName%" 2>&1
for /f "usebackq tokens=*" %%b in ("%_FilePath%%_FileName%") do (
set _LineChk=%%b
if "!_LineChk!"==%OldStr2% (
SET _Found=Found
SET NewStr2=!NewStr2:^"=!
echo !NewStr2!
)
)
echo.
:Exit
exit /b

Batch File - Create Zip file with Command Line using the first part of the first file name

I have a folder of PDF files that have a consistent naming convention. I want to create a zip file of these PDF files but named the zip file using the portion of the file that is before the # -- all of the files are the same in the front (it is the NTID of the user that created the pdf files).
As an example these are what the files might look like in the PDF output folder (there could be 100 files all that start with the same UserID before the #:
UserID#Carlos+Alberto+Mafra-+bribery-2019-05-16
UserID#MAJELA+HOSPITALAR+LTDA-+bribery-2019-05-16
(Ideally, I would also want the current date appended to the zip file)
The zip should be called UserID-2019-05-16.zip based on the example above.
This is the code I am trying to use but not having success...
I created a batch script using others suggestions for each step. but can't get it to work end to end.
FOR %%F IN ("C:\Users\SA-JJC-HCC_Ops\OneDrive - JNJ\workflows\TPIGoogle\pdf\*.pdf") DO (
set filename=%%F
goto next
)
:next
echo "%filename%"
set zipfile=%filename%
for /f "tokens=1 delims=#" %%a in ("%zipfile%") do (
)
cd "C:\Program Files\7-Zip\"
7z.exe" a "C:\Users\SA-JJC-HCC_Ops\JNJ\HCC&P Alteryx - Documents\EPiC\GoogleSearches\zip\" && %zipfile% && ".zip" "C:\Users\SA-JJC-HCC_Ops\OneDrive - JNJ\workflows\TPIGoogle\pdf\*.pdf"
One zip file with all the PDFs that are using the first part of the string from the file names in the PDF folder.
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir"
FOR /f "tokens=1*delims=#" %%a IN (
'dir /b /a-d "%sourcedir%\*#*-????-??-??.pdf" '
) DO (
SET "pre=%%~a"
SET "post=%%~nb"
SET "post=!post:~-10!"
IF DEFINED post ECHO "C:\Program Files\7-Zip\7z" a "%destdir%\!pre!-!post!.zip" "%sourcedir%\%%~a#%%~b"
IF NOT DEFINED post ECHO SKIP "%%~a#%%~b"
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used the variablenames pre and post to ensure that the names used in this process are not keywords like the more logical date.
Read a list of all filenames matching the pattern *#*-????-??-??.pdf in the source directory, tokenising on #. Assign the userid to %%a and thence to pre and the "name" part of the dregs of the actual filename to post, then select only the last 10 characters of post using delayed-expansion.
There is an opportunity here to further process post to check whether it truly fits the pattern for a date, if that is required. That routine may return post either unmolested or empty. If it's not empty then construct the required 7z command (you may wish to add -tzip) and echo this for verification - remove the echo to actuate the 7z compression. If post is emptied by a pattern-checking routine, then the filename will simply be reported as having been skipped.
If it is indeed current date you want to append to the end of the zip file, then we need to get the non locale dependent date and time. This will then copy each of the pdf files that starts with UserID to a zip with a date of the day you run the script UserID-2019-05-17 :
#echo off
set "outDir=C:\Users\SA-JJC-HCC_Ops\JNJ\HCC&P Alteryx - Documents\EPiC\GoogleSearches\zip\"
set "inDir=C:\Users\SA-JJC-HCC_Ops\OneDrive - JNJ\workflows\TPIGoogle\pdf\"
for /f "tokens=1,2 delims==" %%i in ('wmic os get LocalDateTime /VALUE') do (
if ".%%i."==".LocalDateTime." set mydate=%%j
)
set mydate=%mydate:~0,4%-%mydate:~4,2%-%mydate:~6,2%
for %%a in (*.pdf) do for /f "delims=#" %%i in ('dir /b /a-d %%a') do (
"C:\Program Files\7-Zip\7z" a "%outDir%%%i-%mydate%.zip" "%inDir%%%~a"
)
If in fact you want to append the date of the filename instead (In other words create Zip files for each file with a different date as well as matching userid):
#echo off
setlocal enabledelayedexpansion
set "outDir=C:\Users\SA-JJC-HCC_Ops\JNJ\HCC&P Alteryx - Documents\EPiC\GoogleSearches\zip\"
set "inDir=C:\Users\SA-JJC-HCC_Ops\OneDrive - JNJ\workflows\TPIGoogle\pdf\"
for %%a in (*.pdf) do for /f "tokens=1,* delims=#" %%i in ('dir /b /a-d %%a') do (
set fdate=%%~nj
set fdate=!fdate:~-10!
echo "C:\Program Files\7-Zip\7z" a "%outDir%%%i-!fdate!.zip "%inDir%%%~a"
)

How can i properly make this kind of loop?

I have 2 txt files, first file is "recipes.txt", second is "sites.txt" where is website links,
i'm making txt files in a bulk with the names from file "recipes.txt", from second txt file "sites.txt" i want to take rows in a loop and input them into txt file which gonna be created from recipes.txt.
Here i have batch script, which creates files with specific name from "recipes.txt", but its appending only last row of the file "sites.txt".
What shoudl i change to amke it work properly?
#echo off
setlocal enableDelayedExpansion
for /f %%g in (recipes.txt) do (
for /f %%i in (site.txt) do (
(
echo SET !REPLAYSPEED MEDIUM
echo URL GOTO=%%i
)
)>C:\Users\Viktor\Desktop\Sites\Scripts\%%g".iim"
)
pause >nul
just extend your block for redirection to include the for itself:
for /f %%g in (recipes.txt) do (
(
for /f %%i in (site.txt) do (
echo SET !REPLAYSPEED MEDIUM
echo URL GOTO=%%i
)
)>C:\Users\Viktor\Desktop\Sites\Scripts\%%g".iim"
)
pause >nul
Your version overwrites your file with every iteration of %%i

Replace field value in property file using batch

Can anyone help me to replace a field value in property file using batch.
I have this field in my application.properties file: accessKey=AKIAJ2Q and I want to replace it with accessKey=XXXXX
I tried with sed command as recommanded here but it didn't work. The send command is not recognized by windows 10.
I tried also the following code:
SET key="XXXXX"
FOR /F %i IN (application.properties) DO SET accessKey=%key%
pause
Any help would be appreciated.
Thanks
There is surely a way to replace a string inside a txt/properties file using pure batch.
Taking above example to replace accessKey in application.properties
file.
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\<Add directory path of application.properties>"
(
FOR /f "usebackqdelims=" %%a IN ("%sourcedir%\application.properties") DO (
FOR /f "tokens=1*delims==" %%g IN ("%%a") DO (
IF /i "%%g"=="accessKey" (
ECHO(%%g=xxxx
) ELSE (
ECHO(%%a)
)
)
)>application_new.properties
:: This new file now contains a modified version.
rem ECHO(MOVE /y application_new.properties "%sourcedir%\application.properties"
GOTO :EOF
There is no way to "replace" something inside a text file with pure batch. You could use a 3rd party tool like Find'n'Replace or even some PowerShell commands. But what you can do is to "copy" the text file into a temp file while replacing the token you are looking for. Afterwards you can delete the original file and replace it with the temporary one. Here is the code:
#echo off
setlocal enabledelayedexpansion
set sourcefile=something.txt
set tempfile=tempfile.txt
set oldtoken=AKIAJ2Q
set newtoken=XXXXX
type nul>%tempfile%
for /f "tokens=*" %%l in (%sourcefile%) do (
set line=%%l
set line=!line:%oldtoken%=%newtoken%!
echo !line!>>tempfile.txt
)
del %sourcefile%
move %tempfile% %sourcefile%

How to use batch job to add the file "create date" into all the files in a directory?

I have a folder that gets a new file added everyday to the folder with the same file name but incremental extension such as .001, .002, .003, etc. However, if there's no file within the folder it starts at .001 again.
The problem is they are all named the same and if I move them to another folder to archive them it would just overwrite the same file over and over again. I could create a folder each day with the date with only one file in it, but that seems a bit redundant.
Is there a way to look at the create date of each file and rename it to the create date?
I've gotten this far, but it looks like for this situation I have to use a static file name, how to loop through the entire directory?
SET filename = C:\test.001
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
rename c:\test.001 C:\test_%filedatetime%.txt
move C:\*.txt C:\archive\
this provides the correct sort order:
#echo off &setlocal disableDelayedExpansion
set "startfolder=%userprofile%\test"
cd /d "%startfolder%"
for %%a in (*) do (
for /f "delims=." %%b in ('wmic datafile where "name='%startfolder:\=\\%\\%%~a'" get lastmodified^|find "."') do (
echo(ren "%startfolder%\%%~a" "%%~b.txt"
)
)
Remove echo to get it working.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "targetdir=c:\sourcedir"
SET "destdir=c:\destdir"
PUSHD "%targetdir%"
FOR %%a IN (*.*) DO (
SET "timestamp=%%~ta"
SET "timestamp=!timestamp:/=_!
SET "timestamp=!timestamp::=_!
SET "timestamp=!timestamp:.=_!
SET "timestamp=!timestamp:,=_!
SET "timestamp=!timestamp: =_!
ECHO MOVE "%%a" "%destdir%\%%~na.!timestamp!"
)
GOTO :EOF
This should work with any file in the nominated target directory where the name does not include ! or ^.
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
The gymnastics around timestamp are intende to replace /: with _ since these are illegal filename characters. Space., are similarly replaced - they're legal but often painful.
If you want the destination filename to be name.003.timestamp, remove the ~na from the destination name.
Try like this :
SET $path=The_path_who_contain_the_FILES
FOR /F "DELIMS=" %%f IN ('dir "%$path%" /a-d/b') DO (
SET filedatetime=%%~tf
move "%%~dpnxf" "C:\archive\test_%filedatetime%.txt")

Resources