Link to home
Create AccountLog in
Avatar of CalmSoul
CalmSoulFlag for United States of America

asked on

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?
Avatar of Bill Prew
Bill Prew

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

Avatar of CalmSoul

ASKER

Will it be possible to have output logs what got copied?
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
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

@NVIT,

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

~bp
' 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

@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

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

Do you have copy rights to sTgtPath? Can you confirm by creating a file manually there?
@NewVillageIT (NVIT) - yes, I can create file manually
I even added everyone with full control on that folder ...
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

the folder I am copying in is local folder to my machine
@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
@CalmSoul,

I don't get an error here. Can you post a sample of your sSrcPath file?
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
@CalmSoul,

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

~bp
@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 ...
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer