• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 80
  • Last Modified:

batch to modify all files in the directory

I have the following code, I am trying to modify certain line in XML file but I have to do it with all the files in the directory and retain the original file name.

@echo off
setlocal EnableDelayedExpansion

set anotherVariable=yes

(for /F "delims=" %%a in (sampleCopy.xml) do (
   set "line=%%a"
   set "newLine=!line:?>=!"
   if "!newLine!" neq "!line!" (
      set "newLine=<?xml version="1.0" encoding="UTF-8" standalone="%anotherVariable%"?>"
   )
   echo !newLine!
)) > newFile.xml

Open in new window

0
CalmSoul
Asked:
CalmSoul
3 Solutions
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I read "retain the original file name" as "retain the original file", and so we built a new file for each of the original ones.
@echo off
setlocal EnableDelayedExpansion

set anotherVariable=yes

for %%F in (*.xml) do (
  for /F "delims=" %%a in (%%F) do (
     set "line=%%a"
     set "newLine=!line:?>=!"
     if "!newLine!" neq "!line!" (
        set "newLine=<?xml version="1.0" encoding="UTF-8" standalone="%anotherVariable%"?>"
     )
     echo !newLine!
  )
) > %%~dpnF.new.xml

Open in new window

You can provide the path in the first FOR if you like.
0
 
CalmSoulAuthor Commented:
I would like to retain original file name, can we move outputs to different folder?
0
 
CalmSoulAuthor Commented:
If the file name is newFile - Copy (8).xml I have getting error The system cannot find the file newFile.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
@echo off
setlocal EnableDelayedExpansion

set anotherVariable=yes

for %%F in (*.xml) do (
  for /F "usebackq delims=" %%a in ("%%F") do (
     set "line=%%a"
     set "newLine=!line:?>=!"
     if "!newLine!" neq "!line!" (
        set "newLine=<?xml version="1.0" encoding="UTF-8" standalone="%anotherVariable%"?>"
     )
     echo !newLine!
  )
) > "subfolder\%%~nxF"

Open in new window

should solve both issues.
0
 
Flabio GatesCommented:
Something like this?
@echo off
setlocal EnableDelayedExpansion

set anotherVariable=yes

for /R %%x in (*.xml) do call :func "%%x"

goto :EOF

:func
(for /F "delims=" %%a in (%~nx1) do (
   set "line=%%a"
   set "newLine=!line:?>=!"
   if "!newLine!" neq "!line!" (
      set "newLine=<?xml version="1.0" encoding="UTF-8" standalone="%anotherVariable%"?>"
   )
   echo !newLine!
)) > tmp
copy tmp %~nx1 /Y >NUL
del tmp
goto :EOF

Open in new window


EDIT: Guess I was too slow.
0
 
Bill PrewCommented:
I would probably handle it this way.  Specify your source and destination folders at the top.  This code also doesn't require that you be in the source directory when it runs.  It does a little checking on the directories, and if the source does exist then it exits, since it can't process without that folder.  If the destination folder doesn't exist then it creates it.  I also tidied up the line processing in the inner FOR loop to be a little cleaner and efficient. Give this a test there and see how it goes.

@echo off
setlocal EnableDelayedExpansion

rem Define source and destination folders
set BaseDir=B:\EE\EE29075617\old
set DestDir=B:\EE\EE29075617\new

rem Set value for header line variable
set anotherVariable=yes

rem Make sure source directory exists
if not exist "%BaseDir%" (
  echo *ERROR* Source directory does not exist "%BaseDir%"
  exit /b
)

rem Create destination directory if it doesn't exist
if not exist "%DestDir%" md "%DestDir%"

rem Process all XML files in source folder
for %%F in ("%BaseDir%\*.xml") do (

  rem Read each file, line by line
  for /F "usebackq delims=" %%L in ("%%~F") do (

    rem If this line contains "?>" then replace entire line in output file 
    set "line=%%L"
    if "!line:?>=!" neq "!line!" (
      set "line=<?xml version="1.0" encoding="UTF-8" standalone="%anotherVariable%"?>"
    )

    rem Write existing line, or modified line, to new file
    echo !line!
  )

) > "%DestDir%\%%~nxF"

Open in new window


»bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now