Link to home
Start Free TrialLog in
Avatar of Kenny
KennyFlag for Malaysia

asked on

Read a text file and copy each file to a different folder and rename it

Hi,

I need to copy thousands of image files from multiple folders on our server (within 1 main folder) to a new folder and rename it accordingly. All the details are in a text file (saved as tab delimited from an excel sheet) with the following format (excerpt from the actual file) :

\\192.168.1.118\PICTURE\0-PKG\0-PVCBAG\0001-403.GIF      \\192.168.1.118\Picture\B2B-IT\162869_1.jpg
\\192.168.1.118\PICTURE\0-PKG\0-PVCBAG\0001-501-B.JPG      \\192.168.1.118\Picture\B2B-IT\162871_1.jpg
\\192.168.1.118\PICTURE\0-PKG\0-PVCBAG\0002-050.JPG      \\192.168.1.118\Picture\B2B-IT\162878_1.jpg
\\192.168.1.118\PICTURE\0-PKG\0-PVCBAG\0003-116.JPG      \\192.168.1.118\Picture\B2B-IT\162926_1.jpg
\\192.168.1.118\PICTURE\0-PKG\0-B1\0003-114-B1.JPG      \\192.168.1.118\Picture\B2B-IT\162927_1.jpg
\\192.168.1.118\PICTURE\0-PKG\0-PVCBAG\0003-116.JPG      \\192.168.1.118\Picture\B2B-IT\162928_1.jpg
\\192.168.1.118\PICTURE\0-PKG\0-B1\0003-114-B1.JPG      \\192.168.1.118\Picture\B2B-IT\162929_1.jpg
\\192.168.1.118\PICTURE\0-PKG\0-B1\0003-114-B2.JPG      \\192.168.1.118\Picture\B2B-IT\162929_2.jpg

I have mapped the \\192.168.1.118\PICTURE\ folder as P-drive on my PC (from which I hope to run the script) if that is any help. To make it easier to understand, let me give an example here.

For first line line : \\192.168.1.118\PICTURE\0-PKG\0-PVCBAG\0001-403.GIF      \\192.168.1.118\Picture\B2B-IT\162869_1.jpg
The script would need to copy  \\192.168.1.118\PICTURE\0-PKG\0-PVCBAG\0001-403.GIF to folder \\192.168.1.118\Picture\B2B-IT\ and rename the file to 162869_1.jpg
I assume that if typed out, it would be
     copy \\192.168.1.118\PICTURE\0-PKG\0-PVCBAG\0001-403.GIF      \\192.168.1.118\Picture\B2B-IT\162869_1.jpg

However, due to the sheer magnitude of files I am unable to do this manually. I am not familiar with windows scripting. It would be great if I could be pointed to a site where I could refer easily for accurate and comprehensive information about windows scripting.

The server is a physical server running Windows 2008 R2 SP1. Please do let me know if you require more information.

Thank you.

Regards,
Kenny
Avatar of oBdA
oBdA

Not much to do here; save this as Whatever.cmd and run it from a command prompt.
Just change the input file path, and verify that in line 7, there is actually a "tab" character after copying from here.
@echo off
setlocal
set InputFile=C:\Temp\list.txt

REM *** In the next line, between 'Delims=' and the closing double quote '"', MUST BE ONE SINGLE TAB.
for /f "tokens=1,2 delims=	" %%a in ('type "%InputFile%"') do (
	echo Processing '%%~nxa' --^> '%%~nxb'
	copy "%%a" "%%b"
)

Open in new window


And for something more modern, here's the PowerShell version:
$InputFile = 'C:\Temp\List.txt'
Import-Csv -Path $InputFile -Header Source, Target -Delimiter "`t" | ForEach-Object {
	Write-Host "Processing '$([IO.Path]::GetFileName($_.Source))' --> '$([IO.Path]::GetFileName($_.Target))'"
	Copy-Item -Path $_.Source -Destination $_.Target -Force
}

Open in new window

Avatar of Kenny

ASKER

Wow... that was so simple and it worked. Thank you. Unfortunately I missed out 1 detail. The extension is all .jpg in the inputfile. I know that rubbish in equals rubbish out, and I will be giving you the points tomorrow even if you do not help further.

However, is it a lot of work to modify the script to maintain the same extension that was used in the original filename as the extension for the new filename (ignoring the new file extension in the input file?). All extensions are 3 characters only. It would be quite time-consuming for me to change all the wrong extensions in the inputfile as there are over 13,900 records, with many gif and png extensions in addition to jpg.

Thanks so much.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kenny

ASKER

Thank you so much