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
LVL 1
KennyIT Application ExecutiveAsked:
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.

oBdACommented:
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

1
KennyIT Application ExecutiveAuthor Commented:
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.
0
oBdACommented:
Batch:
@echo off
setlocal
set InputFile=C:\Temp\list.txt

REM *** 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" "%%~dpnb%%~xa"
)

Open in new window

Powershell:
$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 "$([IO.Path]::GetDirectoryName($_.Target))\$([IO.Path]::GetFileNameWithoutExtension($_.Target))$([IO.Path]::GetExtension($_.Source))" -Force
}

Open in new window

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
KennyIT Application ExecutiveAuthor Commented:
Thank you so much
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.