I want to book mark and delete the lines from the multiple XML files. I'm new to batch file programming. Could any one pls help me to create the batch file which should read the file and delete the line which contains the string.
Eg: Below is the content of one XML file in which i want to delete the lines which contains "cimage.gif" and "appcover".
<file>/cimages/ncomms_cimage.jpg</file>
<file>/ncomms/2014/141223/ncomms6646/abs/ncomms6646.html</file>
<file>/ncomms/2014/141223/ncomms6646/carousel/ncomms6646-f1.jpg</file>
<file>/ncomms/2014/141223/ncomms6646/carousel/ncomms6646-f2.jpg</file>
<file>/ncomms/2014/141223/ncomms6646/carousel/ncomms6646-f3.jpg</file>
<file>/ncomms/2014/141223/ncomms6646/carousel/ncomms6646-f4.jpg</file>
<file>/ncomms/2014/141223/ncomms6646/carousel/ncomms6646-f5.jpg</file>
<file>/ncomms/2014/141223/ncomms6646/carousel/ncomms6646-f6.jpg</file>
<file>/ncomms/2014/141223/ncomms6646/compound/ncomms6646_ci.html</file>
<file>/ncomms/2014/141223/ncomms6646/covers/appcover-ncomms-v5-ncomms6646-150x193.png</file>
<file>/ncomms/2014/141223/ncomms6646/covers/appcover-ncomms-v5-ncomms6646-450x579.png</file>
<file>/ncomms/2014/141223/ncomms6646/covers/appcover-ncomms-v5-ncomms6646-75x100.png</file>
<file>/ncomms/2014/141223/ncomms6646/extref/ncomms6646-s1.pdf</file>
<file>/ncomms/2014/141223/ncomms6646/extref/ncomms6646-s2.xls</file>
<file>/ncomms/2014/141223/ncomms6646/extref/ncomms6646-s3.xls</file>
<file>/ncomms/2014/141223/ncomms6646/extref/ncomms6646-s4.avi</file>
<file>/ncomms/2014/141223/ncomms6646/fig_tab/ncomms6646_F1.html</file>
<file>/ncomms/2014/141223/ncomms6646/fig_tab/ncomms6646_F2.html</file>
<file>/ncomms/2014/141223/ncomms6646/fig_tab/ncomms6646_F3.html</file>
<file>/ncomms/2014/141223/ncomms6646/fig_tab/ncomms6646_F4.html</file>
<file>/ncomms/2014/141223/ncomms6646/fig_tab/ncomms6646_F5.html</file>
I want to delete both line which contains "appcover" and "cimage"
Coudld you pls update the code n give me? Sorry since Iam new to batch file i'm depending on your coding only.
Also If i want to edit the 10 files i cant put all xmls in one folder since the filename of the all xmls will be pushlive-manifest.xml only, all these files are stored in a their own diretories. Is any way to put this batch file in route folder adn it should read the xmls in the sub-directories with in the root folder.
Eg: route dir path is:D:/abc the xmls are in "D:/abc/123", "D:/abc/121" "D:/abc/125" and so on.
So i can place the batch file in D:/abc folder and if i run batch file it should read the xmls wch are there in "D:/abc/123", "D:/abc/121" "D:/abc/125" and so on.
Expecting your reply on this!
I changed the coding as below:
#ECHO OFF
SETLOCAL
SET "sourcedir=D:\test"
FOR /r "%sourcedir%" %%a IN (pushlive-manifest*.xml) DO (
FINDSTR /v /L "cimage.jpg appcover" "%%a" >"%%~dpna.txt"
move /y "%%a" "%%~na"
)
GOTO :EOF
and as a result it is not processinfg other xmls but it just creates one txt for each xml in its dir. and the out file is not overwiting the original xml it is still generating txt file. this time it is not creating copy of input xml to txt file where as as it is deleting the expecte dlines in the xml and converting it into txt. PLs suggest
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
FOR %%a IN ("%sourcedir%\*.xml") DO (
FINDSTR /v /L "cimage.gif appcover" "%%a" >"%%~dpna.txt"
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
A new file is generated called samebasename.txt in the same directory as the directory scanned. It's not clear whether you want to delete lines that contain either "cimage.gif" **OR** "appcover" or those that contain both "cimage.gif" and "appcover". This code assumes the former.
Revision for revise requirement : search for .xmls in tree
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /r "%sourcedir%" %%a IN (*.xml) DO (
FINDSTR /v /L "cimage.gif appcover" "%%a" >"%%~dpna.txt"
)
GOTO :EOF
Note that in the Windows world, \ is a directory-separator and / is a switch-indicator. Windows often, but not always, makes the translation. Best to use the correct form.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /r "%sourcedir%" %%a IN (pushlive-manifest*.xml) DO (
FINDSTR /v /L "cimage.gif appcover" "%%a" >"%%~dpna.LMX"
Move /y "%%~dpna.LMX" "%%~nxa"
)
GOTO :EOF
This should work for you - just change the directory-name as required
Related
I have a file MyFile.ini with a section in it [MySection]
I want to add a new line with a return after it under that section, via a batch file.
Here's the batch file I last tried:
#echo off
Set file=MyFile.ini
Set section=[MySection]
Set newline=MyNewValue=MyNewSetting
for /f "tokens=*" %%l in (%file%) do (
(echo %%l)>> "%file%"
if /i "%%l"=="%section%" (
(echo %newline%)>> "%file%"
)
)
exit
The above has no effect on the ini file.
I want the ini file to go from this:
[MySection]
SomeExistingValue=SomeExistingSetting
To this:
[MySection]
SomeExistingValue=SomeExistingSetting
MyNewValue=MyNewSetting
Any answer will be greatly appreciated because once I can figure this out I can replicate it and add several settings to my file, it's all a bit tedious doing it manually, especially when I do the exact same thing to the exact same file, every time. The file name never changes, the section always exists, the setting I am adding never exists, so all that stuff does not need to be accounted for, nor does making a backup of the file, I can just unzip my backed up file if it gets messed up.
Here's an example based upon my commented methodology:
#Echo Off
Set "file=MyFile.ini"
Set "section=[MySection]"
Set "newline=MyNewValue=MyNewSetting"
Copy /Y "%file%" "%file%.bak">NUL 2>&1||Exit /B 1
(For /F "Tokens=1*Delims=]" %%G In (
'^""%__AppDir__%find.exe" /N /V ""^<"%file%.bak"^"'
)Do If /I "%%H"=="%section%" (Echo=%%H
Echo %newline%)Else Echo=%%H)>"%file%"
Rem Del "%file%.bak"
I have Remarked the last line, because I think it's more important to ensure that the resultant file, %file%, is correct, before you Delete the original, %file%.bak. The choice to do so ultimately lies with yourself.
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!
At work we use a bespoke program to search through a directory tree and find an individual image file. These files are stored in folders that have a 7-digit name, starting with '18' - so for instance '1873456', '1873457', '1873458' etc. The problem I have is at some point last year the procedure that creates these folders and populates the images in them reached '1899999' - and then rolled over to '18100000' and carried on like that for over 4,000 folders before it was caught and corrected.
The bespoke program we use can only handle seven-digit folder names. What I would like to do is create a batch file that renames all the eight-digit folders by removing the extra '1' in the name, so '18100000' becomes '1800000' and so forth until '18104013' becomes '1804013'.
Can anyone help?
Run this in the base folder, it will not change any folders.
A file called renfile.bat.txt will be created that contains the rename command for the folders that match the filespec. Examine it in notepad to see if it is ok and then rename it to renfile.bat and run it.
It's not tested.
#echo off
setlocal enabledelayedexpansion
for /d /r %%a in (18??????) do (
set "name=%%~nxa"
>>renfile.bat.txt echo ren "%%a" "!name:~0,2!!name:~3!"
)
Something like
for /l %%x in (100000,1,104013) do (
set oldsuffix=%%x
set newsuffix=%oldsuffix:~-5%
ren 18%%x 18%newsuffix%
)
setlocal enableextensions enabledelayedexpansion
for /d /r "c:\somewhere" %%f in (181?????) do (
set "name=0" & set /a "name+=%%~nf" 2>nul
if !name! gtr 1899999 ren "%%~ff" "18!name:~-5!"
)
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")
Here's my situation. A project has as objective to migrate some attachments to another system.
These attachments will be located to a parent folder, let's say "Folder 0" (see this question's diagram for better understanding), and they will be zipped/compressed.
I want my batch script to be called like so:
BatchScript.bat "c:\temp\usd\Folder 0"
I'm using 7za.exe as the command line extraction tool.
What I want my batch script to do is to iterate through the "Folder 0"'s subfolders, and extract all of the containing ZIP files into their respective folder.
It is obligatory that the files extracted are in the same folder as their respective ZIP files. So, files contained in "File 1.zip" are needed in "Folder 1" and so forth.
I have read about the FOR...DO command on Windows XP Professional Product Documentation - Using Batch Files.
Here's my script:
#ECHO OFF
FOR /D %folder IN (%%rootFolderCmdLnParam) DO
FOR %zippedFile IN (*.zip) DO 7za.exe e %zippedFile
I guess that I would also need to change the actual directory before calling 7za.exe e %zippedFile for file extraction, but I can't figure out how in this batch file (through I know how in command line, and even if I know it is the same instruction "cd").
EDIT #1
I have already received some tips on ServerFault to the same question. Please see the answers at this link.
However, it extracted from the root (C:), and not from the given in parameter folder.
Anyone has an idea?
EDIT #2
It seems that batch script doesn't handle folder and file names containing a space character adequately. Can anyone confirm what I think?
EDIT #3
I need it to be fully recursive, since I don't know the directory structure against which this will be used.
EDIT #4.a
With #aphoria's solution, I'm almost there! The only problem is that it takes let's say File5.zip, retrieve the filename only to get File5, creates a subfolder File5 and extract the File5.zip to File5 subfolder, then, it wants to create a File5 subfolder in Folder 1, where it should instead want to create File1 subfolder, to stick with my example.
EDIT #4.b
As required, here's the code as it currently look:
#echo off
setlocal enableextensions enabledelayedexpansion
rem
rem Display instructions when no parameter is given.
rem
if "%1" equ "" (
echo Syntaxe : od.bat ^<directory mask>^
echo Exemple : od.bat *
goto :Eof
)
rem
rem Setting the PATH environment variable for this batch file for accessing 7za.exe.
rem
path=c:\temp;%PATH%
rem
rem Removing quotes from the given command line parameter path.
rem
set root=%1
set root=%root:~%1
set root=%root:~0,-1%
rem Searching directory structure from root for subfolders and zipfiles, then extracting the zipfiles into a subfolder of the same name as the zipfile.
for /F "delims==" %%d in ('dir /ogne /ad /b /s %root%') do (
echo Traitement du dossier : "%%d"
for /F "delims==" %%f in ('dir /b "%%d\*.zip"') do (
rem Getting filename without extension.
set subfolder=~n%f
mkdir "%%d\%subfolder%"
rem Extracting zipfile content to the newly created folder.
7za.exe e "%%d\%%f" -o"%%d\%subfolder%"
)
)
:Eof
endlocal
Ideas anyone?
My guess is that it digs one directory hierarchy at a time. Here's the deal. Consider we have a Folder A in Folder 1 (Folder 1\Folder A), then, it searches from Folder 1 through Folder 5, and comes back to Folder 1\Folder A, where the %subfolder% variable sticks with its last value.
Anyone's help is gratefully appreciated.
I'm not very familiar with the 7zip command-line options, so you will need to figure out the exact command for that, but the script below will take a fully specified path (spaces allowed) and print out the the folder name and .zip files contained within it.
#ECHO OFF
REM
REM Remove the double quotes from the front and end of the root path
REM
SET ROOT=%1
SET ROOT=%ROOT:~1%
SET ROOT=%ROOT:~0,-1%
ECHO %ROOT%
FOR /F "DELIMS==" %%d in ('DIR "%ROOT%" /AD /B') DO (
ECHO %%d
FOR /F "DELIMS==" %%f in ('DIR "%ROOT%\%%d\*.zip" /B') DO (
ECHO %%f
)
)
Run it like this:
MyScript.CMD "c:\temp\usd\Folder 0"
You should get output similar to this:
Folder A
File 1.zip
File 2.zip
Folder B
File 1.zip
File 2.zip
UPDATE
The code below will extract Folder A\File 1.zip to a new folder Folder A\File 1.
A few things to keep in mind:
In the first FOR loop, you need to have %ROOT% enclosed in double quotes to handle folders with spaces in the name.
Since you're SETting a variable inside the second FOR, you need to put SETLOCAL ENABLEDELAYEDEXPANSION at the beginning. Then, reference the variable using ! (for example, !subfolder!) to force expansion at runtime.
This line of your code set subfolder=~n%f should be this set subfolder=%%~nf
I put ECHO in front of the MKDIR and 7za.exe commands to test. Once you are sure it is doing what you want, remove the ECHO statement.
Here is the code:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM
REM Remove the double quotes from the front and end of the root path
REM
SET ROOT=%1
SET ROOT=%ROOT:~1%
SET ROOT=%ROOT:~0,-1%
ECHO %ROOT%
REM Searching directory structure from root for subfolders and zipfiles,
REM then extracting the zipfiles into a subfolder of the same name as the zipfile.
FOR /F "delims==" %%d IN ('dir /ogne /ad /b /s "%ROOT%"') DO (
ECHO Traitement du dossier : "%%d"
FOR /F "delims==" %%f IN ('dir /b "%%d\*.zip"') DO (
REM Getting filename without extension.
SET subfolder=%%~nf
ECHO mkdir "%%d\!subfolder!"
REM Extracting zipfile content to the newly created folder.
ECHO 7za.exe e "%%d\%%f" -o"%%d\!subfolder!"
)
)
ENDLOCAL