Search code examples
powershelldatetimebatch-filecode-conversion

Powershell to executable .bat


I'm trying to get the script below, which is in Powershell, converted to an executable .bat file. The basic idea is that I have a bunch of files of which the file name exists out of 3 parts:

  • Part 1: TKSYSTEMID

  • Part 2: Variable name

  • Part 3: YYYY MM DD HHMM

So a simple example would look like: TKSYSTEMID NOTES YYYY MM DD HHMM

I'm looking for a .bat file that can replace the TKSYSTEM ID and the YYYY MM DD HHMM with something the user gets prompted to input.

The powershell script I have so far is:

$NEWSYSTEMID = Read-Host -Prompt 'Please input SEJ SYSTEM ID' 
Write-Host "Changing TKSYSTEMID to '$NEWSYSTEMID'" 
dir | Rename-Item -NewName {$_.name -replace 'TKSYSTEMID',$NEWSYSTEMID}

$NEWDATETIME = Read-Host -Prompt 'Please input the upload date and time' 
Write-Host "Changing YYYY MM DD HHMM to '$NEWDATETIME'" 
dir | Rename-Item -NewName {$_.name -replace 'YYYY MM DD HHMM',$NEWDATETIME}

Could someone be so kind as to show me how to convert this or to provide a .bat script for this? It would mean the world.


Solution

  • You can wrap the powershell code into batch file like:

    <# : batch portion
    @echo off & setlocal
    
    (for %%I in ("%~f0";%*) do @echo(%%~I) | ^
    powershell -noprofile "$argv = $input | ?{$_}; iex (${%~f0} | out-string)"
    
    goto :EOF
    : end batch / begin powershell #>
    
    $NEWSYSTEMID = Read-Host -Prompt 'Please input SEJ SYSTEM ID' 
    Write-Host "Changing TKSYSTEMID to '$NEWSYSTEMID'" 
    dir | Rename-Item -NewName {$_.name -replace 'TKSYSTEMID',$NEWSYSTEMID}
    
    $NEWDATETIME = Read-Host -Prompt 'Please input the upload date and time' 
    Write-Host "Changing YYYY MM DD HHMM to '$NEWDATETIME'" 
    dir | Rename-Item -NewName {$_.name -replace 'YYYY MM DD HHMM',$NEWDATETIME}