I want to copy latest file from source path to target Path and then rename as below:
Target Path : C;\User\Client1\FinalReports
Source Path: C:\User\Client1\Reports\ReportFolderA (file from Report FolderA should be renamed to target Folder as File1.csv)
C:\User\Client1\Reports\ReportFolderB (file from Report FolderB should be renamed to target Folder as File2.csv)
C:\User\Client1\Reports\ReportFolderD (file from Report FolderD should be renamed to target Folder as File4.csv)
C:\User\Client1\Reports\ReportFolderF (file from Report FolderF should be renamed to target Folder as File5.csv)
"C:\User\Client1\Reports" source Path is fixed followed by variable ReportFolderA, ReportFolderB.etc..so we can set only one source path in the script.
I need a script to selecting the path by browsing pop-up method. where I would select only two path "source & target"
By Browsing pop-up because next time I would have different locations, we can not fix them up in once script. I want to run the script as per need to diffident paths.
Try something like this for copying the newest file from a folder:
@echo off
setlocal
set "src=C:\User\Client1\Reports\ReportFolderA"
set "dst=C:\User\Client1\FinalReports"
pushd "%src%"
for /f "delims=" %%f in ('dir /b /a:-d /o:-d') do (
copy "%%~f" "%dst%\File1.csv"
goto next
)
:next
popd
In VBScript you could use the Shell.BrowseForFolder
method for selecting a folder. Example for selecting the source folder:
Set os = CreateObject("Shell.Application")
basedir = os.Namespace("C:\").Self.Path
Set fldr = os.BrowseForFolder(0, "Select source folder:", &h10&, basedir)
If fldr Is Nothing Then
WScript.Echo "User pressed [Cancel]."
WScript.Quit 1
End If
src = fldr.Self.Path
Finding and copying the most recent file in a folder can be achieved like this:
Set fso = CreateObject("Scripting.FileSystemObject")
Set mostRecent = Nothing
For Each f In fso.GetFolder(src).Files
If mostRecent Is Nothing Then
Set mostRecent = f
ElseIf f.DateLastModified > mostRecent.DateLastModified Then
Set mostRecent = f
End If
Next
If Not mostRecent Is Nothing Then
mostRecent.Copy fso.BuildPath(dst, "File1.csv")
End If
try this:
@echo off &setlocal
set "src=C:\User\Client1\Reports\ReportFolderA"
set "dst=C:\User\Client1\FinalReports"
cd /d "%src%"
for /f "delims=" %%a in ('dir /b /a-d /od') do set "file=%%~a"
copy "%file%" "%dst%\File1.csv"