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?

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

Bill PrewIT / Software Engineering ConsultantCommented:
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
NVITEnd-user supportCommented:
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
CalmSoulAuthor Commented:
Will it be possible to have output logs what got copied?
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Bill PrewIT / Software Engineering ConsultantCommented:
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
NVITEnd-user supportCommented:
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 PrewIT / Software Engineering ConsultantCommented:
@NVIT,

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

~bp
0
NVITEnd-user supportCommented:
' 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
NVITEnd-user supportCommented:
@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
NVITEnd-user supportCommented:
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 PrewIT / Software Engineering ConsultantCommented:
@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
NVITEnd-user supportCommented:
@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 PrewIT / Software Engineering ConsultantCommented:
@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
Bill PrewIT / Software Engineering ConsultantCommented:
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

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