0

I am trying to write a script to get all the file names in a folder without the extension. And then from that txt file with all names I want to copy each file with that name in a folder anywhere. So folder B would have all the files I need with .pdf on it and I want to get the .doc file from folder A with the corresponding name as B

Example Folder B = 123.doc.pdf, 234.doc.pdf, 345.doc.pdf

Folder A = 123.doc, 234.doc, 345.doc, 1324.doc, 54353.doc, 1231.doc

I want to get the names from B without the .pdf and then use that txt file to copy the doc files from folder A in a new folder C.

I created a script to get all file names but not sure how to remove the extension and safe it to the file.

dir /a /b /-p /o:gen >FileNames.txt

Anyone can help?

Ken White
  • 123,280
  • 14
  • 225
  • 444
Slygoth
  • 333
  • 6
  • 17

1 Answers1

0

Here is a simple batch file for this task:

@echo off
md "C:\Temp\Folder C" 2>nul
for %%I in ("C:\Temp\Folder B\*.doc.pdf") do if exist "C:\Temp\Folder A\%%~nI" copy /Y "C:\Temp\Folder A\%%~nI" "C:\Temp\Folder C\" >nul
rd "C:\Temp\Folder C" 2>nul

The second command line creates Folder C with suppressing any error message like the one output on this folder is existing already written to handle STDERR by redirecting it to device NUL.

The third command line uses command FOR to search for any non hidden file in Folder B with file name matching the wildcard pattern *.doc.pdf.

For each found file in Folder B an IF condition is executed to check if a file with same name without file extension .pdf exists in Folder A. If that condition is true the file is copied to Folder C with overwriting an existing file in Folder C with same name, except having read-only attribute set, and with suppressing the success message written to handle STDOUT by redirecting it to device NUL.

Last the Folder C is deleted, but only if being empty. The error message output to handle STDERR on Folder C containing a file is suppressed by redirecting it to device NUL.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • copy /?
  • echo /?
  • for /?
  • if /?
  • md /?
  • rd /?
Mofi
  • 46,139
  • 17
  • 80
  • 143