0

I have many *.bat scripts that I would like to create a GUI for now that I'm learning c#.
The problem is, they just won't execute.
Is there another way I should do this?
StreamWriter maybe or am I forced to just execute a bat file from the windows forms?

Here is one of my *.bat files.

setlocal enabledelayedexpansion
for %%j in (*.*) do (
set filename=%%~nj
set filename=!filename:.=.!
set filename=!filename: =.!
if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"
Echo "!filename!%%~xj"
)

I've tryed all different things, add /c to it, using escape sequenses on every line, using @ infront etc.
I've been told to only use one % in the varibles so that's what I do.
This just gives me "The system cannot find the file specified.".

p.StandardInput.WriteLine("setlocal enabledelayedexpansion");
p.StandardInput.WriteLine("for \"" + myPath + "\" %j in (*.*) do (");
p.StandardInput.WriteLine("set filename=%~nj");
p.StandardInput.WriteLine("set filename=!filename:.=.!");
p.StandardInput.WriteLine("set filename=!filename: =.!");
p.StandardInput.WriteLine("if not \"!filename!\"==\"%~nj\" ren \"%j\" \"!filename!%~xj\"");
p.StandardInput.WriteLine("Echo \"!filename!%~xj\" )");

Here is another try I did, adding & after every command, even tryed && and ||.
This just shuts down before I see any error messages.

psi.Arguments = "/c \"@echo off & setlocal enabledelayedexpansion & for \"" + myPath + "\" %j in (*.*) do ( & set filename = %~nj & set filename = !filename:.=.! & set filename = !filename: =.! & )";
  • Are you saying that you'd like your batch scripts to run, for example, from a button press in a WinForm? – Dillanm May 10 '17 at 11:09
  • @Dillanm - I don't want to use any bat files, just execute the commands straight from windows forms to cmd. –  May 10 '17 at 11:15
  • 2
    this answer http://stackoverflow.com/a/4789324/3110529 might be of use to you then! :) – Dillanm May 10 '17 at 11:19
  • @Dillanm - Thank you, I will take a look at it and see if I can get this right :) –  May 10 '17 at 11:34
  • 1
    check your `for` syntax. `/r` missing? – Stephan May 10 '17 at 11:43
  • @Stephan - That actually did alot better! Now it renames the files. It names all files to !filename! tho :P –  May 10 '17 at 11:49
  • remove the spaces around `=` with your `set` command. They become part of the variable name or value – Stephan May 10 '17 at 11:54
  • with your one-liner: don't use `setlocal enabledelayedexpansion`. Use `cmd` parameter `/v:on` instead. – Stephan May 10 '17 at 12:01
  • 1
    one-liner: `...do ( & command & command &)` doesn't work. Use `...do ( command & command & command)` instead. (remember: you are building *one* line, not multiple lines) – Stephan May 10 '17 at 12:04
  • @Stephan - Thank you for the help, I just can't get it right tho. First set works fine, but 2nd and 3rd dosn't seem to work. set filename=%~nj & set filename=!filename:.=.! & set filename=!filename:= .! –  May 10 '17 at 14:38
  • 1
    pay attention to spaces! Your `filename` is ``. Better use `set` syntax with spaces to avoid: `set "var=value"`. Btw: why are you replacing dots with dots? – Stephan May 11 '17 at 05:08

0 Answers0