Powershell in a batch file - How to avoid metacharacters?

Starting Windows 7, when I copied the file to an external drive, during a regular file backup , I use Powershell v2 (run from a batch file) - create all the timestamps of the source file in the copy file.

The following code works in most cases, but not always: -

SET file=%1
SET dest=E:\

COPY /V /Y  %file% "%dest%"

SetLocal EnableDelayedExpansion
FOR /F "usebackq delims==" %%A IN ('%file%') DO (
      SET fpath=%%~dpA
      SET fname=%%~nxA
)

PowerShell.exe (Get-Item \"%dest%\%fname%\").CreationTime=$(Get-Item \"%fpath%%fname%\" ^| Select-Object -ExpandProperty CreationTime ^| Get-Date -f \"MM-dd-yyyy HH:mm:ss\")

The above code copies the file and then sets the date / time the copy (destination) file was created to the file of the source file when I drag the source file into my batch file.

, . "", () [...], " CreationTime" ". "".

, &.

Powershell, , . , - , , Powershell.

, . .ps1, , , .ps1.

.

. , , mklement0. , Powershell -

PowerShell.exe (Get-Item -LiteralPath \"%dest%\%fname%\").CreationTime=$(Get-Item -LiteralPath \"%fpath%%fname%\" ^| Select-Object -ExpandProperty CreationTime ^| Get-Date -f \"MM-dd-yyyy HH:mm:ss\")

, ( Windows 7):

  • . .

    • : , : ; ,
      powershell.exe -command echo \"a b\" a b.
      "..." -
      powershell.exe -command "echo \"a b\"" - cmd.exe , ; ,
      powershell.exe -command "echo \"a & b\""
  • , ( ), . FAT NTFS, .

  • '( ) Powershell, NTFS, . , , , NTFS.

  • ROBOCOPY - , ! ( = `^). EVEN, ( ):

    ROBOCOPY "%fpath% " "%dest%" "*%name%*%ext%*" /B /COPY:DAT /XJ /SL /R:0 /W:0 /V

    . "% fpath%" ESSENTIAL, .

    . , , (!).

    . FILENAME, .

+4
3

:

Windows 7 robocopy.exe , ( , ):

@echo off
:: Do NOT use setlocal ENABLEDELAYEDEXPANSION, because it would cause
:: misinterpretation of  "!" chars. in filenames.    
setlocal

:: Parse the file path given as %1 (the first argument) into its folder path and filename.
:: Be sure to pass the %1 argument *double-quoted* to prevent up-front interpretation 
:: by cmd.exe; e.g.:
::   someBatchFile "c:\tmp\foo.txt" or someBatchFile "%file%"
:: Note that %~dp1 always returns a path with a trailing "\".
set "fpath=%~dp1"
set "fname=%~nx1"

:: Determine the destination folder
set "dest=E:\"

:: Use robocopy to copy the file to the destination dir. with timestamps preserved.
:: Syntax is: <source-dir> <dest-dir> <filename-or-wildcard> ...
:: IMPORTANT: To avoid problems with paths that end in "\", always follow
::            a variable reference inside "..." with a *space* (a trick discovered by
::            Ed999 himself).
robocopy "%fpath% " "%dest% " "%fname%"

:

  • robocopy , , , "%fname%" .
    , robocopy - PowerShell - [ ] wildcard , (, d , * ?, ).

  • (, "%fpath% "), robocopy - - \" ", . , , \ (, "E:\\"), , . , - "%var% " ( ) .

  • robocopy , /V, , , , - verify on .


PowerShell:

powershell -command "(Get-Item -LiteralPath '%dest%%fname%').CreationTime=(Get-Item -LiteralPath '%fpath%%fname%').CreationTime"

. , '. ( /), , (, %fname:'=''% %fname% ' ):

powershell -command "(Get-Item -LiteralPath '%dest:'=''%%fname:'=''%').CreationTime=(Get-Item -LiteralPath '%fpath:'=''%%fname:'=''%').CreationTime"
  • , "...", cmd.exe (, &), - . [1]

  • '...' , , PowerShell ( "..." , $), , ).

  • -LiteralPath , Get-Item , -Path, -, -Path , PowerShell, [ ].

  • .CreationTime + ; .CreationTime, [System.DateTime].


[1] :

  • \ - , (\"%dest%\%fname%\"), , , , , .

  • , "..." , cmd.exe , , & ; ,
    powershell.exe -command "echo \"a b\"" , ,
    powershell.exe -command "echo \"a & b\"" , - &.

    • \"" \" , :
      powershell.exe -command "echo \""a & b\""" a & b
  • '...' PowerShell "..." - : ' .

+2

, .

100% , REN SET Windows, ROBOCOPY POWERSHELL.

@echo off

::  **  INPUT File **
    SET file=%1

::  **  Destination Directory **
    SET dest=C:\Users\%Username%\Desktop\test

::  **  Copy using Command Shell **
    COPY /V /Y  %file% "%dest%"

::  **  Location of PowerShell **
    SET PowerShell=C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -NoProfile

::  ** Store PATH & NAME of source file (WITHOUT quotation marks) **
    FOR /F "usebackq delims==" %%A IN ('%file%') DO (
          SET fpath=%%~dpA
          SET fname=%%~nxA
    )

::  ** Rename the Files **
    REN "%fpath%%fname%" temp1
    REN "%dest%\%fname%" temp2

::  ** Set CREATED date of output file **
::  NB: Will fail if MONTHS (MM) is identical to MINUTES (mm)
    %PowerShell% (Get-Item \""%dest%\temp2\"").CreationTime=$(Get-Item \""%fpath%temp1\"" ^| Select-Object -ExpandProperty CreationTime ^| Get-Date -f \"MM-dd-yyyy HH:mm:ss\")

::  ** Set MODIFIED date of output file **
::  NB: Will fail if MONTHS (MM) is identical to MINUTES (mm)
    %PowerShell% (Get-Item \""%dest%\temp2\"").LastWriteTime=$(Get-Item \""%fpath%temp1\"" ^| Select-Object -ExpandProperty LastWriteTime ^| Get-Date -f \"MM-dd-yyyy HH:mm:ss\")

::  ** Wait **
    ::  No, I have no idea why it doesn't work without this...
    echo. & echo Wait 15 Seconds ... & echo.
    @CHOICE /T 15 /C yn /D y > NUL

::  ** Restore ORIGINAL filenames **
    REN "%fpath%temp1" "%fname%"
    REN "%dest%\temp2" "%fname%"
0

, , , "" .

, , ( !) , ( , : , , Powershell Robocopy).

- , , - . "" , !

Windows 7, Windows.

Windows 7 "send" to the folder:

C: \ Users \% Username% \ AppData \ Roaming \ Microsoft \ Windows \ SendTo

.

@echo off

::  *** Copy file including its CREATED date & MODIFIED date ***

::  File : Drag-and-Drop
    SET file=%1

::  Destination Directory
    SET dest=E:\

::  ** Safety Checks **
    ATTRIB -R -A -S -H  %file%

::  ** Store PATH & NAME of file (WITHOUT quotation marks) **
    FOR /F "usebackq delims==" %%A IN ('%file%') DO (
      SET fpath=%%~dpA
      SET fname=%%~nxA
      SET  name=%%~nA
      SET   ext=%%~xA
    )

    ::  *** POWERSHELL : File only ***

    ::  ** Copy File **
    COPY /V /Y  %file% "%dest%"

    ::  ** Location of PowerShell **
    SET PowerShell=C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -NoProfile

    ::  ** Set CREATED date of copy **
    ::  NB: Will fail if MONTHS (MM) is identical to MINUTES (mm)
    %PowerShell% -command "(Get-Item -LiteralPath '%dest:'=''%\%fname:'=''%').CreationTime=(Get-Item -LiteralPath '%fpath:'=''%%fname:'=''%').CreationTime"

    ::  ** Set MODIFIED date of copy **
    ::  NB: Will fail if MONTHS (MM) is identical to MINUTES (mm)
    %PowerShell% -command "(Get-Item -LiteralPath '%dest:'=''%\%fname:'=''%').LastWriteTime=(Get-Item -LiteralPath '%fpath:'=''%%fname:'=''%').LastWriteTime"

    ::  ** Set ACCESSED date of copy **
    ::  NB: Will fail if MONTHS (MM) is identical to MINUTES (mm)
    %PowerShell% -command "(Get-Item -LiteralPath '%dest:'=''%\%fname:'=''%').LastAccessTime=(Get-Item -LiteralPath '%fpath:'=''%%fname:'=''%').LastAccessTime"

    ::  Open Destination Directory
    C:\Windows\Explorer.exe "%dest%"
0
source

Source: https://habr.com/ru/post/1683638/


All Articles