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

LVL 5
CalmSoulAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", 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
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Qlemo"Batchelor", 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.