J.Do
J.Do

Reputation: 201

xCopy into the top folder of a directory

I running an xcopy command to transfer from one file to the other.

xcopy /s "c:\users\documents\thisfile.txt" "d:\otherfiles\1.2.1"

I'd like to be able to just copy the file into the most recent folder in the otherfiles directory rather than hard coding it every time a new version folder is created. These are versions numbers and these tend to just increase.

Is this entirely possible?

Upvotes: 0

Views: 570

Answers (2)

user7818749
user7818749

Reputation:

Ok, it is possible to check the versions of the directories, but that will take a bit more code as we cannot simply remove the dots to get a numeric value and compare to the next. The reason being, considering versions 1.2.3 and 1.23 if we remove the dots to make it a matchable numeric value, both these will end up being being 123 therefore each version section would need to be tested.

However, based on your comments to my questions, you create new versions as folders, and therefor it ia sortable by date, so simply run a dir command and sort by created date. It will set the latest folder as the variable you need:

@echo off
for /f "delims=" %%i in ('dir /b /ad /o:d D:\otherfiles') do set "myvar=%%i"
xcopy /s "c:\users\documents\thisfile.txt" "d:\otherfiles\%myvar%"

Upvotes: 1

lit
lit

Reputation: 16266

If you wanted to do this in PowerShell, it is possible. This would require PowerShell 3.0 or higher. It can be done with 2.0, but would require changes. Hopefully, you are on or can upgrade to a modern-day version of PowerShell.

When you are confident that the file will be copied correctly, remove the -WhatIf from the Copy-Item cmdlet.

$fn = 'C:/src/t/xxx.txt'
$destbasedir = 'C:/src/t/lastdir'

Get-ChildItem -Directory -Path $destbasedir |
    Sort-Object -Property Name |
    Select-Object -Last 1 |
    ForEach-Object { Copy-Item -Path $fn -Destination $_.FullName -Whatif }

This could be put into a .bat file script.

SET "FN=C:\src\t\xxx.txt"
SET "DESTBASEDIR=C:\src\t\lastdir"

powershell -NoProfile -Command ^
    "Get-ChildItem -Directory -Path %DESTBASEDIR% |" ^
        "Sort-Object -Property Name |" ^
        "Select-Object -Last 1 |" ^
        "ForEach-Object { Copy-Item -Path "%FN%" -Destination "$_.FullName" -Whatif }"

Upvotes: 1

Related Questions