Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Change PowerShell script to eliminate 4 second pausing if file(s) not present in source

I would like to change this script so that only if the file exists, it performs the copy and then pauses for 4 seconds.  

If the *.csv file(s) do not exist, then it should move on to the .bat file to perform the next function. 


Thanks in advance for your help. 


pushd "E:\Users\Users1\Inbound\"
if exist *.csv copy /b E:\Users\Users1\InboundCopy\*.csv amalgamated.csv
popd


ping -n 4 -w 1000 127.0.0.1 > nul
....

Open in new window


Avatar of Nemanja Jovic | MVP
Nemanja Jovic | MVP
Flag of Serbia image

Hey, something like


if (Test-Path 'pathToFile) {
   copy task here
}
else {
   bat task here
}

Open in new window



Avatar of oBdA
oBdA

Same as before: you test, and if it doesn't exist, you goto to the next statement:
pushd "E:\Users\Users1\Inbound\"
if not exist *.csv goto End_Copy
	copy /b E:\Users\Users1\InboundCopy\*.csv amalgamated.csv
	ping -n 4 -w 1000 127.0.0.1 > nul
:End_Copy
popd

Open in new window

Though again, I don't see the need for the pause (unless you want to have a bit of time to see the results).
The copy will be done when the call to copy returns, this is nothing that continues in the background.
Avatar of E=mc2

ASKER

Thakns oBdA, 

I tried using the same type of script that was presented yesterday with *.csv files but it did not copy if I remember well.

Thanks again. 

The reason might be that you have two different folders:
you "pushd" to "E:\Users\Users1\Inbound", but you copy from "E:\Users\Users1\InboundCopy"
If they are supposed to be the same, you might want to change that to something like
set source=E:\Users\Users1\Inbound
if not exist "%source%\*.csv" goto End_Copy
	copy /b "%source%\*.csv" "%source%\amalgamated.csv"
	ping -n 4 -w 1000 127.0.0.1 > nul
:End_Copy

Open in new window

Edit: Fixed target path
Avatar of E=mc2

ASKER

Thanks oBDA,

They should be 2 different folders since teh *.csv file or files are in the source or at the push d folder, yet it needs to be copied to the destination folder which is different. 

The goal is to take all *.csv files and copy them to a new folder into a new file named amalgamated.csv

Hope this helps. 


ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of E=mc2

ASKER

Yes that's right the copy needs to be to a different folder to amalgamate the files. 

Thanks again, will try this.

And also in this case you don't feel we need the pause?

Not for technical reasons. Only if you want it there to have a moment to spot potential errors.
Avatar of E=mc2

ASKER

Ok thanks so much oBdA.. let me give it a try..