Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

General discussion

Copying newest file else keep destination folder empty

Sep 9, 2013 1:05PM PDT

I would like a batch file in command line or written in VBSCRIPT for the following scenario:

There are 2 folders:

1-IncomingData 2-NewestData

The 1-IncomingData folder has several .txt files in it, new files arrive in this folder every day.

When I run the script, I would like to accomplish the following:
1.Copy the newest/most recent text file from 1-IncomingData to 2-NewestData - only - if a new file is found in 1-IncomingData since the last time the script ran, otherwise do not copy it into 2-NewestData (in which case you would leave 2-NewestData folder empty)

The script below is incomplete and I cannot accomplish what I am looking for:

pushd "C:\Users\CP\Desktop\Data\1-IncomingData\" for /f "tokens=* delims= " %%G in ('dir/b/od') do (set newest=%%G) copy %newest% C:\Users\CP\Desktop\Data\2-NewestData\ popd

fc /b "%newest%" C:\Users\CP\Desktop\Data\2-NewestData>nul 2>&1 if %errorlevel% equ 0 ( del /q "%newest%" ) else ( copy /y "%newest%" C:\Users\CP\Desktop\Data\2-NewestData\ ) popd

Any assistance would be greatly appreciated. Thanks in advance.

Discussion is locked

- Collapse -
Wouldn't a sync app do this?
Sep 9, 2013 2:00PM PDT

While you could write code, it sounds like a sync app like GoodSync, SyncBack LE could be set to do this.
Bob

- Collapse -
Example links to learn more about VBScript.
Sep 9, 2013 2:03PM PDT
- Collapse -
Copying newest file else keep destination folder empty
Sep 9, 2013 8:53PM PDT

Thanks for the suggestion.
I need it in code since I would like to run it in a command line within an existing batch file.
Some extra code would be useful.

- Collapse -
Said code examples were found in my second search.
Sep 10, 2013 1:24AM PDT

I rarely give anyone code here because some want me to support it, explain it or miss the big issue that you must understand your code.
Bob

- Collapse -
Re: copying
Sep 9, 2013 9:46PM PDT

Can you detail your requirements. Say there are 3 new files in folder 2 (one from 13:00, one from 13:15 and one from 14:23), then you want your script to only copy the one from 14:23 and leave the older two untouched?

In other words: your "if a new file is found" is only a partial requirement. It doesn''t tell what to do when there is more than one new file. And with incomplete requirements it's unlikely you'll get the solution you need.

Kees

- Collapse -
Copying newest file else keep destination folder empty
Sep 10, 2013 7:31AM PDT

I will clarify further..

Yes, in your scenario, I want the script to leave the other 2 files untouched, and only look at the newest file (the most recent file) which was written into the 1-Incoming Data folder. I want it to be able to realize that there is new file in the 1-Incoming Data folder and when I first run the script to copy it to the 2-NewestData folder. When running the script again, if when it looks at the 1-IncomingData folder and sees the file which it had copied the first time to the 2-NewestData folder and is the same, then I want it to delete the file in the 2-NewestData folder.
In fact every time the script runs, if it has already copied the newest file into the 2-NewestData folder form the 1-IncomingData folder, I want the 2-NewestData folder to stay empty - until of course a more newer/more recent file is written into the 1-Incoming Data folder...

I hope this clarifies things.

- Collapse -
Re - copying
Sep 10, 2013 7:41AM PDT

Yes, I will clarify..

No matter how many more recent files it finds in the 1-IncomingData folder, I would like the script, when I run it the first time as it were, to check the 1-IncomingData folder for the newest/most recent file. Then copy that file to the 2-NewestData folder. When I run the script again, I want it to be able to look inside the 1-IncomingData folder and see if the file that it had already copied is in the same as the one in the 2-NewestData folder - if it is, then empty the 2-NewestData folder. When I run the script again, I want it to keep the 2-NewestData folder empty -until- a more recent file is found in the 1-IncomingData folder.

As you can probably tell, ,my goal is to use a script which looks inside the 2-NewestData folder and if it sees a file in there I want it to be able to use that file etc...

- Collapse -
Re: copying
Sep 17, 2013 11:44PM PDT

The FileSystemObject in vbscript seems to have all everything you need. Just start writing the code.

Kees