I have a batch file process that runs and creates a file. When it was originally written it had a static folder path to create a file. I have modified the script to set a datestamp variable that creates a folder with the datestamp as the name of the folder then places the file inside of that folder.
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value')
do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "datestamp=%YYYY%-%MM%-%DD%"
set "dirOutput=C:\Temp\Output\%datestamp%"
mkdir %dirOuput%
There is another process that runs an executable that has a configuration file that has the original hard coded folder path in it.
<appsetings>
<add key="Target" value="C:\Temp\Output"/>
</appsettings>
Is there a way that I can modify the configuration file for the executable to match the folder path with the datestamp variable that I created in the batch file?
ie.
<appsetings>
<add key="Target" value="C:\Temp\Output\(Current date)"/>
</appsettings>
Related
I'm trying to create a folder in windows with current timestamp details and copy some folder to it. I tried as below:
bat 'for /f "tokens=2-4 delims=/ " %%i in ("%date%") do SET today_fname=%%i_%%j_%%k'
bat 'for /f "tokens=2-4 delims=/ " %%i in ("%date%") do md today_fname'
bat 'cd %today_fname%'
bat 'copy "C:/Program Files (x86)/Jenkins/workspace/jenkins Pipeline/application/bin/Debug/netcoreapp2.1/os/publish"'
It ends up creating a folder with a timestamp name and copying the folder contains to current directory instead of Cd to newly created folder
I'm trying to create a folder with a name 05_14_18_7_31 and copy the contains present in this location C:/Program Files (x86)/Jenkins/workspace/jenkins Pipeline/application/bin/Debug/netcoreapp2.1/os/publish to 05_14_18_7_31
Here is something you can try:
#echo off
rem Create datestamp:
set "datestamp=%date:~4,-8%_%date:~7,-5%_%date:~12,2%"
rem Request for me, if you are not using `dd/mm/yy` format, to provide another script for your occassion.
rem Create timestamp:
set "timestamp=%time:~0,2%_%time:~3,2%"
rem Create folder:
md %datestamp%_%timestamp%
xcopy /E "C:/Program Files (x86)/Jenkins/workspace/jenkins Pipeline/application/bin/Debug/netcoreapp2.1/os/publish" "%datestamp%_%timestamp%"
Hope this helps!
I am trying to get lastmodified data using a batch file across a network
the line of code I am using to get the info is
wmic datafile where name="%file:\=\\%" get lastmodified
This works fine on local files but I get a No Instance(s) Available error when I try to use that line on a network file such as \\networkshare\folder\file.txt.
datafile does not support UNC paths.
You have two options
Connect to the remote node and ask for the file, knowing the local path to the file in the remote system
set "file=e:\somewhere\file.txt"
wmic /node:serverName datafile where name="%file:\=\\%" get lastmodified
If you don't know the local paths in the remote system, it is necessary to map a drive letter to the network share to retrieve the needed information
Sample code
#echo off
setlocal enableextensions disabledelayedexpansion
rem Configure file
set "file=\\server\share\folder\file.txt"
rem Separate path and filename
for %%a in ("%file%") do ( set "filePath=%%~dpa" & set "fileName=%%~nxa" )
rem Change to target path and adapt file path if sucessful
pushd "%filePath%" && (for %%a in (.\) do set "filePath=%%~fa")||(set "filePath=")
rem If the current directory has changed, get file data and return to previous folder
if defined filePath (
wmic datafile where "name='%filePath:\=\\%%fileName%'" get LastModified
popd
)
My requirement is to take backup of only those files which have been modified or created recently.
At this point of time my code is as below. It is picking up the overall xml files in the destination folder under datetime folder.
#echo off
for /F "tokens=1-4 delims=/ " %%A in ('date/t') do (
set DateDay=%%A
set DateMonth=%%B
set DateYear=%%C
)
for /F "tokens=1-4 delims=/ " %%D in ('time/t') do (
set DateTime=%%D
)
set CurrentDate=%DateDay%-%DateMonth%-%DateYear%-%time:~0,2%.%time:~3,2%
xcopy "D:\Splunk\etc\apps\search\default\data\ui\views\*.xml" "\\cidcswpems2533\d$\backup\%CurrentDate%\"
Help of command xcopy output by entering xcopy /? in a command prompt window lists a parameter which you have to append here on last line and you are done.
/m
Copies source files that have their archive file attributes set. Unlike /a, /m turns off archive file attributes in the files that are specified in the source.
The archive file attribute is designed for backup processes. Every time a file is created new or modified, the archive attribute is automatically set. Therefore copying just files with archive attribute set and remove it after copy helps you here to copy just the files modified or created since last backup process.
I want to replace the content paths defined into the file i.e logging.properties to the desired location path of the jboss7 location .
Basically i'm using installer where i have to browse my jboss7 folder and locate it to any desired location of the user . But in few files of jboss7 there are some hardcoded path defined like in given logging.properties file.
I need to change that hard coded path to the desired location path.
As of now i'm having repl.bat and file test.bat files in the same folder.
repl.bat helper file could be find in following link:-
http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
I just copied the code and created repl.bat file.
test.bat file :-
#ECHO OFF
SETLOCAL
SET "folder="
FOR /r "C:\" %%a IN (tintin.txt) do IF EXIST "%%a" SET "folder=%%~dpa"&GOTO got1
FOR /r "D:\" %%a IN (tintin.txt) do IF EXIST "%%a" SET "folder=%%~dpa"&GOTO got1
:got1
echo "%folder%"
PAUSE
set "newpath=%folder%"
set "newpath=%newpath:\=\\%"
echo "%newpath%"
PAUSE
type "logging.properties" | repl "(Directory=).*(\\\\standalone\\\\)" "$1%newpath%$2">"logging.properties.tmp"
PAUSE
move "logging.properties.tmp" "logging.properties"
PAUSE
GOTO :EOF
PAUSE
Here in this test.bat file , i'm searching a file tintin.txt file and setting the path into a variable name as 'folder'. tintin.txt file is just inside the folder of jboss7.This is because of the possibilities of more than one jboss7 application server folder into the system.
Till now i have got the path i.e "C:\Users\Anuj\Desktop\jboss7\" and set into the variable 'folder'.
Now there is file named logging.properties into the folder location
C:\Users\Anuj\Desktop\jboss7\standalone\configuration
logging.properties :-
com.latilla.import.uploadDirectory=C:\\progra~2\\Latilla\\C4i\\jboss7\\ standalone\\uploads
com.latilla.import.maxFilesUploadNumber=10
com.latilla.export.templateFile=C:\\progra~2\\Latilla\\C4i\\jboss7\\standalone\\templates\\GDV_HDI_Format.xls
com.latilla.etl.pluginsRootDirectory=C:\\progra~2\\Latilla\\C4i\\jboss7\\standalone\\cloverETL\\plugins
com.latilla.etl.templatesDirectory=C:\\progra~2\\Latilla\\C4i\\jboss7\\standalone\\etl
com.latilla.db.user=postgres
com.latilla.db.pass=password
repl.bat helper file helps to replace the url path with the desired path i.e path set to variable name 'folder'.
I want to replace the C:\progra~2\Latilla\C4i\jboss7\ with the path set to variable name 'folder'.
Note :-
here in logging.properties file path contents is having different format of path i.e C:\
means double slash. \
Might be the script that i have tried test.bat is incorrect.
When i double click the test.bat file i got error.
Although I can't help you with fixing the issue you are getting while using the repl.bat file, I can suggest a different way of solving the initial problem of path replacement.
If the jboss7 string is guaranteed to be present in all the original paths in your configuration file(s), you could try the following approach:
#ECHO OFF
SETLOCAL DisableDelayedExpansion
FOR /F "delims=" %%A IN ('DIR /B /S C:\tintin.txt') DO (CD /D "%%~dpA" & CALL :got1)
FOR /F "delims=" %%A IN ('DIR /B /S D:\tintin.txt') DO (CD /D "%%~dpA" & CALL :got1)
GOTO :EOF
:got1
SET "propfile=%CD%\standalone\configuration\logging.properties"
IF NOT EXIST "%propfile%" GOTO :EOF
SET "tempfile=%TEMP%\logging.properties.tmp"
FIND /I /V "jboss7\\" >"%tempfile%"
>>"%tempfile%" (
FOR /F "tokens=1,* delims=" %%I IN ('FIND /I "jboss7\\"') DO (
SET "pathname=%%J"
SETLOCAL EnableDelayedExpansion
IF NOT "!pathname!" == "!pathname:*jboss7\\=!" (
SET "pathname=%__CD__:\=\\%!pathname:*jboss7\\=!"
)
ECHO %%I=!pathname!
ENDLOCAL
)
)
ECHO Old file "%propfile%":
TYPE "%propfile%"
ECHO =======================================
ECHO New file:
TYPE "%tempfile%"
PAUSE
:: uncomment the next line once you have verified the replacement works correctly
::MOVE "%tempfile%" "%propfile%"
Searching for the tintin.txt file has been changed slightly so as to possibly make the process faster. Instead of iterating over every directory and checking if it contains the file, the loops now read the output of DIR, which returns only actually existing entries.
Note that you could also use a FOR /R loop, as in your present code, with the same effect i.e. returning only existing paths, but the IN clause would need to contain a mask rather than a normal name, but that would have to be a mask that couldn't match anything else in your system than just tintin.txt. For instance, if you knew for certain that there could be no file called tintin.txt1 or tintin.txtx or anything else where tintin.txt is followed by exactly one character, you could use the following template instead:
FOR /R "C:\" %%A IN (tintin.txt?) DO (CD /D "%%~dpA" & CALL :got1)
and same for D:\. That would return only references to files actually existing and matching the mask.
Also, you can see that the loops do not jump (GOTO) to the got1 label but instead call the got1 subroutine. With that change, it is possible to process many application instances in one go. I don't know yours can be installed multiple times. If not, you'll probably want to change it back to GOTO.
The subroutine in my script is referencing the config file using its full path as specified in your description (...\standalone\configuration\logging.properties). For some reason, in your script the file is referenced simply by its name, even though there's no preceding CD or PUSHD command changing the current directory to the location of the file. I assumed you were trying to simplify your script and omitted that bit, whether intentionally or not. Otherwise I may have missed something in your explanation and/or script.
After verifying that the config file exists at the expected location, the replacement itself is done in this way:
All the non-path config lines are written to a temporary file with one go.
Every config line containing a path is processed in this way:
if it does not contain the jboss7\\ string, it is omitted;
otherwise the part of the path up to and including jboss7\\ is removed;
the current directory is inserted before the remaining part (after every \ is replaced with \\);
the new value is put back into the config line;
the update line is added to the same temporary file.
The old version is of the configuration file replaced with the new one.
Obviously, the script may change the order of lines in the processed file, but it is assumed that that doesn't matter.
I want to write a DOS batch file to delete old folders in a directory but keep the newest folder. Here is a sample of my folders under D:\myfolders\:
Jack_20110507 (previous day time-stamped folder)
Jack_20110508 (current day time-stamped folder)
James_20110507
James_20110508
Kenny_20110507
Kenny_20110508
...
I would like to delete all previous day time-stamped folders *_20110507, but keep all current day time-stamped folders *_20110508. New timestamped folders are created daily.
The script below will get the current local date in YYYYMMDD (courtesy of this answer).
It then loops through all the folders in a given directory (In my case this was a test sub folder beneath the folder containing the script) and checks the last 8 characters of the folder name. If they do not match today's date, it deletes the folder.
The /Q disables the confirmation for folder deletion. If you want to be prompted for confirmation before a folder is deleted, remove this.
#ECHO off & setlocal EnableDelayedExpansion
REM Get the local datetime
FOR /F "usebackq tokens=1,2 delims==" %%i IN (`wmic os get LocalDateTime /VALUE 2^>NUL`) DO IF '.%%i.'=='.LocalDateTime.' SET ldt=%%j
REM Set the datetime to YYYYMMDD format.
SET today=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%
REM For every file in the folder test:
FOR /D %%f IN (.\test\*) DO (
REM Store the folder name temproarily
SET folder=%%f
REM Get just the ending date part of the folder name
SET folderpart=!folder:~-8!
REM If it doesn't match todays date, delete the folder.
IF NOT !folderpart!==!today! RD /Q %%f
)
#ECHO ON
I wouldn't use a batch script for anything other than the very simplest stuff.
I recommend you look at a scripting language such as Python.