I have 2 folders in folder called C:\durvi\mmi_test\mmidurvi which are
C:\durvi\mmi_test\mmidurvi\durvyauu
C:\durvi\mmi_test\mmidurvi\sgdf
Both these folders have Connections.xml file
I would like to replace any occurance of ql99015 with dd32261
A sample format of file is as below
<pre><anyType xsi:type="xsd:string">ql99015</anyType>
<anyType xsi:type="xsd:string">ql99015_flowreeng_Anthony</anyType> </pre>
I tried something like below but does not work:
for /D %%f in (c:\durvi\mmi_test\mmidurvi\*) do (
cd %%f
if not exist "Connections.xml" (echo this file does not exist)&goto :eof
SETLOCAL=ENABLEDELAYEDEXPANSION
ren "Connections.xml" "Connections1.xml"
for /f %%a in (Connections1.xml) do (
set write=%%a
echo %%a
if !write!=="ql99015" set write="dd32261"
echo !write! >> Connections.xml
)
del "Connections1.xml"
cd..
)
Thanks for your help in advance!
Change the script to:
SETLOCAL ENABLEDELAYEDEXPANSION
for /r %%a in (connections.xml) do (
move "%%a" "%%a.temp"
for /f "usebackq tokens=*" %%b in ("%%a.temp") do (
set write=%%b
echo !write:ql99015=dd32261! >> "%%a"
)
del "%%a.temp"
)
Related
I need to rename .txt files with information contained in them. I can't make the ren command works. The remaining code seems correct, the 'string' is correct too.
Any help and remark is welcome. Thank you.
EDIT: the ren command isn't working but instead displaying "Filename already exists, or can't find the file".
If I replace ren "%%F" !string! by ren "%%F" "example.txt" the first .txt file of my folder will be correctly renamed.
#echo off
pause
Set "ActualFolder=D:\folder"
cd /d %ActualFolder%
Setlocal EnableDelayedExpansion
FOR %%F IN (*.txt) DO (
echo.
ECHO Previous name: %%F
FOR /F "tokens=5" %%T IN ('FINDSTR /C:"name1" %%F') DO (
SET "n1=%%T"
)
FOR /F "tokens=3" %%T IN ('FINDSTR /C:"name2" %%F') DO (
SET "n2=%%T"
)
FOR /F "tokens=5" %%T IN ('FINDSTR /C:"name3" %%F') DO (
SET "n3=%%T"
)
SET string="!n1! !n2! !n3!.txt"
echo New name: !string!
ren "%%F" !string!
)
PAUSE
Hello i have a batch script but i cant work out how to change the path to scan all subfolders within the directory.
In other words i dont want -
C:\Users\ally\Desktop\Documents\Table\CSV versions\2014\
but rather:
C:\Users\ally\Desktop\Documents\Table\CSV versions
as there are lots of different years of data in seperate folders.
Also to note within year folder there are month folders and within that there are the csv files.
#echo off
setlocal enabledelayedexpansion
set "target=C:\Users\ally\Desktop\Documents\All"
cd /d "C:\Users\ally\Desktop\Documents\Table\CSV versions\2014\"
for /L %%a in (101,1,148) do (
set num=%%a
del "%target%\-!num:~-2!.csv" 2>nul
>"%target%\-!num:~-2!.csv.txt" echo Type,angle,edge,Date,Frame,Sum,Mafe,Comp,Rim,Dose,Ell,Role
)
for %%a in (*.csv) do (
for /f "skip=1 usebackq delims=" %%b in ("%%a") do (
for /f "tokens=1,2 delims=-," %%c in ("%%b") do (
set "line=%%c"
if /i "!line:~0,2!"=="HH" >> "%target%\-%%d.csv.txt" echo %%b
)
)
)
ren "%target%\*.csv.txt" *.
pause
To process every folder under the All tree then you can use a for /d /r loop and pushd/popd to set the current folder.
This assumes that every folder has the files you want to process.
Test this on a copy of your data and change the folder to point to it.
You seem to be deleting .csv files, creating .csv.txt files and then trying to process *.csv in the lower loop. Should that be *.csv.txt ?
#echo off
setlocal enabledelayedexpansion
for /d /r "c:\Users\ally\Desktop\Documents\All" %%z in (*) do (
pushd "%%z"
for /L %%a in (101,1,148) do (
set num=%%a
del "-!num:~-2!.csv" 2>nul
>"-!num:~-2!.csv.txt" echo Type,angle,edge,Date,Frame,Sum,Mafe,Comp,Rim,Dose,Ell,Role
)
for %%a in (*.csv) do (
for /f "skip=1 usebackq delims=" %%b in ("%%a") do (
for /f "tokens=1,2 delims=-," %%c in ("%%b") do (
set "line=%%c"
if /i "!line:~0,2!"=="HH" >> "-%%d.csv.txt" echo %%b
)
)
)
ren "*.csv.txt" *.
popd
)
pause
Try adding another loop that will walk down the directory tree and if it finds a csv file, it will process it:
#echo off
setlocal enabledelayedexpansion
set "target=C:\Users\ally\Desktop\Documents\All"
for /f "tokens=1* delims=" %%D in ('dir /s /b /o:-n /a:d "C:\Users\ally\Desktop\Documents\Table\CSV versions"') do (
cd /d "%%~fD"
if exist *.csv (
for /L %%a in (101,1,148) do (
set num=%%a
del "%target%\-!num:~-2!.csv" 2>nul
>"%target%\-!num:~-2!.csv.txt" echo Type,angle,edge,Date,Frame,Sum,Mafe,Comp,Rim,Dose,Ell,Role
)
for %%a in (*.csv) do (
for /f "skip=1 usebackq delims=" %%b in ("%%a") do (
for /f "tokens=1,2 delims=-," %%c in ("%%b") do (
set "line=%%c"
if /i "!line:~0,2!"=="HH" >> "%target%\-%%d.csv.txt" echo %%b
)
)
)
ren "%target%\*.csv.txt" *.
)
)
pause
Explanation of the dir switches:
/s - Displays files in specified directory and all subdirectories.
/b - Uses bare format (no heading information or summary).
/o:in - Lists the files in reverse order
/a:d - Displays only folders
Explanation of the %%~fD: expands %A to a fully qualified path name (from for /?)
I am new to command scripting.
I am able to find and list out the missing files from a folder into an output file using the below code.
Please let me know how can i force to get an error code 1 when there is a missing file.
Thanks
#echooff
setlocal enabledelayedexpansion
pushd "N:\opasdata\d110001\medias\images"
set found=false
for /f "tokens=* delims=" %%a in (listimagescopy.txt) do (
for /r %%x in (%%a) do (
if exist "%%a" set found=true
)
if "!found!"=="false" echo %%a >>"V:\Current Library\notfound.txt"
set found=false
)
ECHO Files are Available
EXIT /B 0
:END
ECHO Files are not Available
EXIT /B 1
#echooff
setlocal enableextensions disabledelayedexpansion
pushd "N:\opasdata\d110001\medias\images"
set "missingFiles=0"
for /f "delims=" %%a in (listimagescopy.txt) do (
dir /s /b /a-d "%%a" >nul 2>nul || (
echo %%a >> "V:\Current Library\notfound.txt"
set "missingFiles=1"
)
)
if %missingFiles%==0 (
echo Files are available
) else (
echo Files are not available
)
popd & endlocal & exit /b %missingFiles%
i have a lot of folders , with in then there are files with name : XXXX_transcoded.j2c
i need to remove the _transcoded.j2c from the file and update to XXXX.txt
anyone have any idead how to do it
here my code so far
for /r %%i in (*.j2c) do (
call:Set %%~ni
)
:Set
set currenttext=%*
set currenttext=%currenttext:_transcoded=%
echo %currenttext%
%%~ni.Contract.xml %%i.txt
pause
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*_transcoded.j2c" '
) DO (
SET "fullname=%%a"
SET "oldname=%%~nxa"
CALL :changename
)
GOTO :EOF
:changename
SET "newname=%oldname:_transcoded.j2c=%"
ECHO REN "%fullname%" "%newname%.txt"
GOTO :eof
This should work for you. You'd have to set your directory into sourcedir.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
#echo off
setlocal disableDelayedExpansion
for /f "eol=: delims=" %%F in (
'dir /s /b /a-d *_transcoded.j2c'
) do (
set "full=%%F"
set "name=%%~nxF"
setlocal enableDelayedExpansion
ren "!full!" "!name:~0,-15!.txt"
endlocal
)
This should be all you need. Test it on some sample files.
#echo off
for /r %%i in (*_transcoded.j2c) do (
for /f "delims=_" %%a in ("%%~nxi") do ren "%%i" "%%a.txt"
)
I searched extensively and found what I believe to be a solution to my problem, which is merging CSV files without duplicating headers each time. It looks like it works, except it's only copying the first file in the folder into the destination file. I think it's unable to open the files because they have a space in the name. I've been advised I probably just need to put quotes somewhere, but I'm not sure where they would go. Thanks in advance.
#ECHO OFF
SET first=y
SET newfile=new.csv
for %%F in (*.csv) do IF NOT %%F==%newfile% (
if defined first (
COPY /y "%%F" %newfile% >nul
set "first="
) else (
FOR /f "skip=1delims=" %%i IN (%%F) DO >> %newfile% ECHO %%i
)
)
#echo off
setlocal enableextensions disabledelayedexpansion
rem configure paths
set "source=*.csv"
set "target=newfile.csv"
rem remove output file if needed
if exist "%target%" del "%target%" >nul 2>nul
rem search for header row
set "headerRow="
for %%f in ("%source%") do (
<"%%~ff" ( for /l %%a in (1 1 10) do if not defined headerRow set /p "headerRow=" )
if defined headerRow goto haveHeader
)
:haveHeader
if not defined headerRow (
echo ERROR: impossible to get header row.
goto endProcess
)
rem output header to header file to use as filter.
rem header is cut to avoid findstr limitations on search strings
set "headerFile=%temp%\%~nx0_headerFile.tmp"
setlocal enableextensions enabledelayedexpansion
> "%headerFile%" echo(!headerRow:~0,125!
endlocal
rem search for input files with matching headers to join to final file
for /f "tokens=*" %%f in ('findstr /m /b /l /g:"%headerFile%" "%source%"') do (
if not exist "%target%" (
rem first file is directly copied
copy "%%~f" "%target%" /y > nul 2>nul
) else (
rem next files are filtered to exclude the header row
findstr /v /b /l /g:"%headerFile%" "%%~f" >> "%target%"
)
echo ... [%%~ff] joined to %target%
)
rem remove the temporary header file
del "%headerFile%" >nul 2>nul
:endProcess
endlocal
Here's another option.
#echo off
set "newfile=new.txt"
del "%newfile%" 2>nul
for %%a in (*.csv) do (
if not exist "%newfile%" (type "%%a" > "%newfile%") else (more +1 "%%a" >> "%newfile%")
)
ren "%newfile%" "new.csv"
#echo off &setlocal disableDelayedExpansion
set "NewFile=new.csv"
>"%NewFile%" cd .
for /f "tokens=1*delims=:" %%a in ('dir /b /a-d /od *.csv ^|findstr /nvx "%NewFile%"') do (
if %%a equ 1 (
copy /b "%%~b" "%NewFile%" >nul
) else (
for /f "skip=1delims=" %%c in ('type "%%~b"') do >>"%NewFile%" echo(%%c
)
)
sed for Windows
I think the line near the end starting "FOR /f" is mixed up and it should be:
#ECHO OFF
SET first=y
SET newfile=new.csv
for %%F in (*.csv) do IF NOT %%F==%newfile% (
if "%first%"=="y" (
COPY /y "%%F" %newfile% >nul
set "first="
) else (
FOR /f "skip=1delims=" %%i IN ("%%F") DO ECHO %%i >> %newfile%
)
)
#ECHO OFF
SET first=y
SET "newfile=new.txt"
del new.csv 2>nul >nul
for %%F in (*.csv) do (
if defined first (
COPY /y "%%F" %newfile% >nul
set "first="
) else (
FOR /f "usebackqskip=1delims=" %%i IN ("%%F") DO >> %newfile% ECHO %%i
)
)
ren %newfile% new.csv
The set "var=value" syntax ensures that any trailing spaces on the batch line are not included in the value assigned to var.
First step is to delete the new.csv file - the 2>nul >nul redirects messages and error messages from del so that the command is totally silent - whether the file exists or not.
Next, you don't need to check whether the new.csv is selected as %%F because it's just been deleed if it did exist, and the output is now to new.txt (filename not critical - actually, I'd be tempted to call it new.vsc. The critical thing is that it isn't .csv so for doesn't need to check it)
Other than the first file (a copy is faster than reading and echoing), the name of the file (in %%F) being read into %%i, since it needs to be "quoted" (to tell CMD that the spaces are not separators) you need to add the usebackq to the for/f controls.
Finally, rename your file to the desired new name.
This should fix the problem.