Best Users,
I want to made a batch script that deletes a specific word in a txt file.
I used the following but it didn't work
(Script that I used) :
SET /p word=device
FOR /f "delims=" %%a IN ('dir /a-d /b "Input_%date%.txt"') DO (
SET "fname=%%~na"
SET "fname=!fname:%word%=!"'
IF NOT "!fname!"=="" REN "%%~a" "!fname!%%~xa"
(Input_%date%.txt):
1015faf2da091b02 device
1115fbd4e0dd3b03 device
I want to delete the word "device" in every line.
Can anyone help me out!
Kind Regards,
A.V.R
I guess you want sth like this:
SETLOCAL EnableDelayedExpansion
set x=device
FOR /f "delims=" %%a IN (Input_%date%.txt) DO (
set b=%%a
echo !b:%x%=!
)
But once you install sed (from for example unxutils) you can do this with simple one liner
type input_%date%.txt | sed "s/device//"
I assumed you can forward output to another file afterwards.
Related
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"
)
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%
I need some help with batch files as I have not done it before. I need to find textfiles with a matching pattern and move them to a class folder. I have a couple of folders that I am browsing through. Can anyone help me to modify the code to move the textfiles together?
This is what i research and piece together.
#echo off
setlocal enabledelayedexpansion
set file=name.txt
set foldername=class
set location=blockB
for /f "tokens=* delims=" %%a in (!file!) do (
set folder=%%a\public\
if not exists "%CD%\!folder!\!foldername!" (mkdir "%CD%\!folder!\!foldername!") ELSE (call)
dir /b "!folder!" | findstr /r /c"!location!"
)
my name.txt contains (adam,ben,charlie) 1 name per each line. So technically i want to move text files in (\adam\public) which contains blockB in the textfile name to a newly created folder call class (\adam\public\class). i want it to do the same for (\ben\public) and (\charlie\public). If I have any mistake in the way i code pls pardon me. Thanks.
Reason why I did not use a full path is because I am going to use it on different computers with same folder configurations.
I guess you are trying to accomplish this:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FILE=name.txt"
set "FOLDERNAME=class"
set "LOCATION=blockB"
for /F "usebackq eol=| delims=" %%F in ("!FILE!") do (
set "FOLDER=%%~fF\public"
mkdir "!FOLDER!\!FOLDERNAME!"
move "!FOLDER!\*!LOCATION!*.txt" "!FOLDER!\!FOLDERNAME!\"
)
endlocal
I have hundreds of csv files . csv files are stored in folders and sub folders . I want to search fifty csv file whose file names have been determined , for example 1.csv , 2.csv , 3.csv , ... , 50.csv . very troublesome if I searched one by one using the Windows search tool . I would like if the files are found , save in the folder named FOUND . please help to overcome this problem by using the batch programming / bat ? thank you very much
There's a number of approaches one can take, depending on how much automation you require... To help you get started, you may want to look at this it helped me (and indeed continues to do so) when I started learning batch. Furthermore I will provide one possible template for achieving your objective, as I have interpreted it. Perhaps it is not the most elegant or efficient method, but it introduces a number of batch commands that you may or may not have encountered, which in turn may help you develop your own method.
#echo off
setlocal enabledelayedexpansion
echo Please enter a drive letter:
set /p "drive=>"
echo Please enter a search string:
set /p "searchstring=>"
echo %searchstring%>search.txt
set /p search=<search.txt
set /a suffix=0
echo.>>search.txt
:LOOP
for /f "tokens=*" %%i in ("search.txt") do (
set /a suffix=suffix+1
set seq=%search% !suffix!
echo !seq!>>search.txt
)
if !suffix! leq 49 goto LOOP
for /f "tokens=*" %%i in (search.txt) do (
for /f "tokens=*" %%j in ('dir /b /s /a-d %drive%:\"%%i.csv" 2^>nul') do (
if not exist "%~dp0\found" md "%~dp0\found"
move /y "%%j" "%~dp0\found\%%~nxj"
)
)
pause
This is not intended as a definitive solution, though you may find it answers your original query/request. All the best.
Here's another working solution for you..
#ECHO OFF
SETLOCAL EnableDelayedExpansion
REM First Set your directories input and output
SET InputDir=C:\Directory to your CSV files\
SET OutputDir=C:\Directory to your CSV files\FOUND
REM check if the FOUND directory exist, if not, then create it.
IF NOT EXIST OutputDir (
mkdir %OutputDir%
)
REM Grab a scan of the input directory and save it to a temporary file list.
Dir /a /b %InputDir%>"%OutputDir%\Found.txt"
REM Set the files you would like to find.
SET "File1=1.csv"
SET "File2=2.csv"
SET "File3=50.csv"
REM The loop, to process the matching file(s).
FOR %%A IN (%File1%,%File2%,%File3%) DO (
FOR /F "usebackq" %%B IN ("%OutputDir%\Found.txt") DO (
IF %%A==%%B (
copy "%InputDir%\%%A" "%OutputDir%\%%A"
)
)
)
REM Clean up the temp file list.
DEL "%OutputDir%\Found.txt"
Make note, I didn't add quotes to the Input and Output variables, but instead added quotes to the copy portion of the code to compensate for white spaces in your directory path. I tried to keep it simple, so you could follow the logic of how it processed what you are looking for, you can now modify this to your liking.. Have fun. Cheers!
I have a utility (myexefile.exe) which outputs a list of information like this:
Line1=[Information in line1]
Line2=[Information in line2]
Line3=[Information in line3]
Line4=[Information in line4]
, etc.
I use a .bat file to write this information to a text file like this:
set myexefile="c:\myexefile.exe"
set outputfile="c:\outputfile.txt"
%myexefile% >> %outputfile%
However, I would like to write all of the lines except for the line containing "Line3=".
So I want the output to the outputfile.txt to be:
Line1=[Information in line1]
Line2=[Information in line2]
Line4=[Information in line4]
, etc.
I could probably create the file as it is and then use an existing sample which shows how to remove a line from a text file, but I would rather skip the line in the first place, rather than writing it to a text file and then removing it.
Can someone help me with this?
%myexefile% | find /v "Product ID">> %outputfile%
should filter out any line containing Product ID
setLocal enableDelayedExpansion
set outputfile="c:\outputfile.txt"
for /f "delims=" %%a in ('c:\myexefile.exe') do (
set out=%%a
if "!out:~0,4!" NEQ "Line3" echo %%a>>%outputfile%
)
or alternatively -
set outputfile="c:\outputfile.txt"
for /f "delims=" %%a in ('c:\myexefile.exe') do for /f %%b "delims=:" in ("%%a") do if "%%b" NEQ "Line3" echo %%a>>%outputfile%