batch script help

Hello Experts:

I am looking for simple VB script to read file path from file1.txt and copy each of them (files) to different folder on the network

Does anybody has anything they can share?
LVL 5
CalmSoulAsked:
Who is Participating?
 
Bill PrewCommented:
Okay, this should handle that.

@echo off
setlocal

set DestDir=H:\destdir
set FileList=file1.txt
set LogFile=copylog.txt
set MissingFile=missing.txt

echo Started: %DATE% %TIME%>>"%LogFile%"

for /f "tokens=* usebackq" %%A in ("%FileList%") do (
  if exist "%%~A" (
    echo %DATE% %TIME% - Copying: "%%~A" to "%%~A">>"%LogFile%"
    copy "%%~A" "%%~A"
  ) else (
    echo %DATE% %TIME% - Missing file: "%%~A">>"%MissingFile%"
  )
)

echo Ended: %DATE% %TIME%>>"%LogFile%"

Open in new window

~bp
0
 
Bill PrewCommented:
No nead for a VBS for this, a small BAT script can do the job.  Save as a BAT, adjust filenames near top, and give it a test.

@echo off
setlocal

set DestDir=H:\destdir
set FileList=file1.txt

for /f "tokens=* usebackq" %%A in ("%FileList%") do (
  copy "%%~A" "%DestDir%"
)

Open in new window

~bp
0
 
NVITCommented:
If he really wants VBS, then...
' Copy files in a list to a folder

Const OverwriteExisting = TRUE
Const ForReading = 1

sTgtPath="\\server\share\"
sSrcPath="c:\dir\file1.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    (sSrcPath, ForReading)

Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    Wscript.Echo "Copying " & strNextLine & " to " & sTgtPath
    objFSO.CopyFile strNextLine , sTgtPath, OverwriteExisting
Loop

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CalmSoulAuthor Commented:
Will it be possible to have output logs what got copied?
0
 
Bill PrewCommented:
This will add some logging to the file specified.

@echo off
setlocal

set DestDir=H:\destdir
set FileList=file1.txt
set LogFile=copylog.txt

echo Started: %DATE% %TIME%>>"%LogFile%"

for /f "tokens=* usebackq" %%A in ("%FileList%") do (
  echo Copying: "%%~A" to "%%~A">>"%LogFile%"
  copy "%%~A" "%%~A"
)

echo Ended: %DATE% %TIME%>>"%LogFile%"

Open in new window

~bp
0
 
NVITCommented:
VBS. Added logging.

' Copy files in a list to a folder

Const OverwriteExisting = TRUE
Const ForReading = 1, ForWriting = 2, ForAppending = 8

sTgtPath="\\server\share\"

sSrcPath="c:\dir\file1.txt"
sLog="%temp%\copyfilesLog.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set obSrcFN = objFSO.OpenTextFile (sSrcPath, ForReading)
Set obLogFN = objFSO.OpenTextFile (sLog, ForAppending, True)

Do Until obSrcFN.AtEndOfStream
    strNextLine = obSrcFN.Readline
    arrServiceList = Split(strNextLine , ",")
    Wscript.Echo Now & " " & "Copying " & strNextLine & " to " & sTgtPath
    objFSO.CopyFile strNextLine , sTgtPath, OverwriteExisting
    obLogFN.WriteLine Now & " " & strNextLine
Loop

Open in new window

0
 
Bill PrewCommented:
@NVIT,

I think you forgot the close of the log file, which is critical.

~bp
0
 
NVITCommented:
' Copy files in a list to a folder

Const OverwriteExisting = TRUE
Const ForReading = 1, ForWriting = 2, ForAppending = 8

sTgtPath="\\server\share\"

sSrcPath="c:\dir\file1.txt"
sLog="c:\dir\copyfilesLog.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set obSrcFN = objFSO.OpenTextFile (sSrcPath, ForReading)
Set obLogFN = objFSO.OpenTextFile (sLog, ForAppending, True)

Do Until obSrcFN.AtEndOfStream
    strNextLine = obSrcFN.Readline
    arrServiceList = Split(strNextLine , ",")
    Wscript.Echo Now & " " & "Copying " & strNextLine & " to " & sTgtPath
    objFSO.CopyFile strNextLine , sTgtPath, OverwriteExisting
    obLogFN.WriteLine Now & " " & strNextLine
Loop
obSrcFN.Close
obLogFN.Close

Open in new window

0
 
CalmSoulAuthor Commented:
@NewVillageIT (NVIT) - on your script I am getting error permission denied
C:\temp\readncopy.vbs(19, 5) Microsoft VBScript runtime error: Permission denied

Open in new window

0
 
CalmSoulAuthor Commented:
@Bill Prew - on your batch file ...
nothing get copys.. and when I looked at the logs it was copying from target to target
0
 
NVITCommented:
@CalmSoul

Do you have copy rights to sTgtPath? Can you confirm by creating a file manually there?
0
 
CalmSoulAuthor Commented:
@NewVillageIT (NVIT) - yes, I can create file manually
0
 
CalmSoulAuthor Commented:
I even added everyone with full control on that folder ...
0
 
NVITCommented:
Can you change sTgtPath temporarily, to a local folder that also has write rights? Then test. e.g.
sTgtPath="c:\testfolder\"

Open in new window

0
 
CalmSoulAuthor Commented:
the folder I am copying in is local folder to my machine
0
 
Bill PrewCommented:
@CalmSoul,

@Bill Prew - on your batch file ...
nothing get copys.. and when I looked at the logs it was copying from target to target
What do the lines in your text file look like, full path, or just a file name?

What folder did you run the batch script from?

Can you paste up a few lines from the output log?

~bp
0
 
NVITCommented:
@CalmSoul,

I don't get an error here. Can you post a sample of your sSrcPath file?
0
 
CalmSoulAuthor Commented:
Bill- I was able to fix your script and got it working now... Question will it be possible to capture instance file not exist in different log file..

I want to capture file path.. Thanks
0
 
Bill PrewCommented:
@CalmSoul,

I don't understand the question you are now asking, can you explain further please.

~bp
0
 
CalmSoulAuthor Commented:
@Bill: During the copy process, if source file doesn't exist .. I get output "file not found" or something.. Is it possible to track that output in separate log file ...
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.

All Courses

From novice to tech pro — start learning today.