1

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 2
    Why do you need a batch file if you have a PowerShell script? Batch files are scripts for the old Windows command prompt. PowerShell is the new (and better) command prompt. Both script types are executable. – Martin Liversage Apr 03 '17 at 04:51
  • Sorry I wasn't aware that powershell can be made an executable. I just need a simple option to double click and run through the prompt to rename the files, but the way it is right now I can't seem to make it an executable. (I don't have a lot of knowledge regarding these things so I came here looking for advise haha) – Priscilla Dimitriou Apr 03 '17 at 05:29
  • Possible duplicate of [Is there any way to make powershell script work by double clicking .ps1 file?](http://stackoverflow.com/questions/10137146/is-there-any-way-to-make-powershell-script-work-by-double-clicking-ps1-file) – Martin Liversage Apr 03 '17 at 05:51

4 Answers4

2

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}
npocmaka
  • 55,367
  • 18
  • 148
  • 187
1

The Batch file below is practically a direct translation of your original PowerShell code, with the difference that each file is renamed only once.

@echo off
setlocal EnableDelayedExpansion

set /P "NEWSYSTEMID=Please input SEJ SYSTEM ID: "
set /P "NEWDATETIME=Please input the upload date and time: "
echo Renaming files
for /F "tokens=*" %%a in ('dir /B') do (
   set "name=%%a"
   set "name=!name:TKSYSTEMID=%NEWSYSTEMID%!"
   set "name=!name:YYYY MM DD HHMM=%NEWDATETIME%!"
   ren "%%a" "!name!"
)

However, if the "Part 2" in the file names is comprised by a single word, like NOTES, then the code may be simpler, because the "Parts 1, 2 and rest" (tokens 1,2*) are directly taken in the rename process:

@echo off
setlocal

set /P "NEWSYSTEMID=Please input SEJ SYSTEM ID: "
set /P "NEWDATETIME=Please input the upload date and time: "
echo Renaming files
for /F "tokens=1,2*" %%a in ('dir /B') do (
   ren "%%a %%b %%c" "%NEWSYSTEMID% %%b %NEWDATETIME%"
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

Windows doesn't, per default, allow running Powershell scripts by double-clicking a .PS1 file. This actually is a security feature aimed at protecting the less-computer savvy users. Back in the days, VBScript files were a popular attack vector for all kind of malware. This was as VBScript files were runnable by default wit a double-click. It was all too easy to fool inexperienced users to get infected with, say, Love Letter worm.

As a work-around, create a shortcut that launches Powershell's shell and passes parameter to the script. In case of link rot, :

powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"
Community
  • 1
  • 1
vonPryz
  • 22,996
  • 7
  • 54
  • 65
0

I started using the solution of @npocmaka, but had trouble encoding non-US-characters. The following solution solves that.

<# : Save as UTF8 without BOM
@ECHO OFF
powershell.exe -NoProfile -Command "Get-Content -Path \"%~f0\" -Encoding UTF8 -Raw | Invoke-Expression"
PAUSE
EXIT /B %errorlevel%
:Script part below #>
Set-StrictMode -Version Latest

Write-Host("Hello Germans: ÄÖÜäöü")
Andreas
  • 1,997
  • 21
  • 35