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

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
0
Kenny
Asked:
Kenny
  • 2
  • 2
1 Solution
 
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
 
KennyIT Application ExecutiveAuthor Commented:
Thank you so much
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

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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