Need Batch Script - New Question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need Batch Script - New Question

#31 Post by foxidrive » 03 May 2013 10:38

You have to escape *every* poison character.


Or place the text in a file called file.txt and use this:

Code: Select all

@echo off
for /f "delims=" %%a in (file.txt) do echo(%%a
pause

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Need Batch Script - New Question

#32 Post by Aacini » 03 May 2013 15:46

The Batch file below is another possible way to activate the GUI pop-up that allows the user to select the folder:

Code: Select all

@if (@CodeSection == @Batch) @then

@echo off
setlocal

rem Place your original Batch code here

rem Activate the pop-up GUI and get the folder
for /F "delims=" %%a in ('CScript //nologo //E:JScript "%~F0"') do (
   set selectedFolder=%%a
)

rem Process the selected folder in any way you wish
echo Selected folder: "%selectedFolder%"
goto :EOF

End of Batch section


@end


// JScript section

// Creates a dialog box that enables the user to select a folder and display it.
var title = "Select a folder", rootFolder = 5;
var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, title, 0, rootFolder);
WScript.Stdout.WriteLine(folder ? folder.self.path : "");


Reference: Shell.BrowseForFolder method


Antonio

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Need Batch Script - New Question

#33 Post by Aacini » 03 May 2013 16:09

Ops! Why he did changed the topic at the middle of it! (bad idea :( )

Ok, new answer to new topic!

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set i=0
for %%a in ("  ______   __       __  ________   ______    ______   __       __  ________ "
            " /      \ /  |  _  /  |/        | /      \  /      \ /  \     /  |/        |"
            "/$$$$$$  |$$ | / \ $$ |$$$$$$$$/ /$$$$$$  |/$$$$$$  |$$  \   /$$ |$$$$$$$$/ "
            "$$ |__$$ |$$ |/$  \$$ |$$ |__    $$ \__$$/ $$ |  $$ |$$$  \ /$$$ |$$ |__    "
            "$$    $$ |$$ /$$$  $$ |$$    |   $$      \ $$ |  $$ |$$$$  /$$$$ |$$    |   "
            "$$$$$$$$ |$$ $$/$$ $$ |$$$$$/     $$$$$$  |$$ |  $$ |$$ $$ $$/$$ |$$$$$/    "
            "$$ |  $$ |$$$$/  $$$$ |$$ |_____ /  \__$$ |$$ \__$$ |$$ |$$$/ $$ |$$ |_____ "
            "$$ |  $$ |$$$/    $$$ |$$       |$$    $$/ $$    $$/ $$ | $/  $$ |$$       |"
            "$$/   $$/ $$/      $$/ $$$$$$$$/  $$$$$$/   $$$$$$/  $$/      $$/ $$$$$$$$/"
           ) do (
   set /A i+=1
   set "line[!i!]=%%~a"
)

for /L %%i in (1,1,%i%) do echo !line[%%i]!


Antonio

MeSH
Posts: 30
Joined: 17 Feb 2013 09:58

Re: Need Batch Script - New Question

#34 Post by MeSH » 03 May 2013 18:43

Aacini wrote:Ops! Why he did changed the topic at the middle of it! (bad idea :( )

Ok, new answer to new topic!

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set i=0
for %%a in ("  ______   __       __  ________   ______    ______   __       __  ________ "
            " /      \ /  |  _  /  |/        | /      \  /      \ /  \     /  |/        |"
            "/$$$$$$  |$$ | / \ $$ |$$$$$$$$/ /$$$$$$  |/$$$$$$  |$$  \   /$$ |$$$$$$$$/ "
            "$$ |__$$ |$$ |/$  \$$ |$$ |__    $$ \__$$/ $$ |  $$ |$$$  \ /$$$ |$$ |__    "
            "$$    $$ |$$ /$$$  $$ |$$    |   $$      \ $$ |  $$ |$$$$  /$$$$ |$$    |   "
            "$$$$$$$$ |$$ $$/$$ $$ |$$$$$/     $$$$$$  |$$ |  $$ |$$ $$ $$/$$ |$$$$$/    "
            "$$ |  $$ |$$$$/  $$$$ |$$ |_____ /  \__$$ |$$ \__$$ |$$ |$$$/ $$ |$$ |_____ "
            "$$ |  $$ |$$$/    $$$ |$$       |$$    $$/ $$    $$/ $$ | $/  $$ |$$       |"
            "$$/   $$/ $$/      $$/ $$$$$$$$/  $$$$$$/   $$$$$$/  $$/      $$/ $$$$$$$$/"
           ) do (
   set /A i+=1
   set "line[!i!]=%%~a"
)

for /L %%i in (1,1,%i%) do echo !line[%%i]!


Antonio


about your post above this.. sorry :( but i gave up on that :( i don't get it :( i think i need to dig more for gaining knowledge... :(

btw for second post... thanks :) i'll try that now :) update later :)

EDIT:

thanks! it work! :) and now I will insert this on my main script and let's see if it will work :)

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Need Batch Script - New Question

#35 Post by Aacini » 04 May 2013 11:01

MeSH wrote:about your post above this.. sorry :( but i gave up on that :( i don't get it :( i think i need to dig more for gaining knowledge... :(

My code is a substitute of abc0502's one. I taken the program you posted under "is this correct sir?" comment and replaced the :VBS: code by my own. Here it is:

Code: Select all

@if (@CodeSection == @Batch) @then

@echo off
title TEST
:MAIN
echo Testing files (Y/N)
set "cho="
set /p "cho=>"

if /i %cho%==Y goto INSTALL1
if /i %cho%==N goto EXIT
echo Invalid choice
goto MAIN

:INSTALL1
mkdir UserBackup
for /F "delims=" %%a in ('CScript //nologo //E:JScript "%~F0"') do (
   set Destination_Folder=%%a
)
XCopy "NewFolder\NewFolder_2\*.*" "%Destination_Folder%\*.*" /c /s /r /d /y /i > "UserBackup\Logfile_NewFolder_2.log"
pause>nul
:EXIT
exit

@end

// Creates a dialog box that enables the user to select a folder and display it.
var title = "Select a folder", rootFolder = 0;
var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, title, 0, rootFolder);
WScript.Stdout.WriteLine(folder ? folder.self.path : "");


Antonio

MeSH
Posts: 30
Joined: 17 Feb 2013 09:58

Re: Need Batch Script - New Question

#36 Post by MeSH » 04 May 2013 23:40

Aacini wrote:
MeSH wrote:about your post above this.. sorry :( but i gave up on that :( i don't get it :( i think i need to dig more for gaining knowledge... :(

My code is a substitute of abc0502's one. I taken the program you posted under "is this correct sir?" comment and replaced the :VBS: code by my own. Here it is:

Code: Select all

@if (@CodeSection == @Batch) @then

@echo off
title TEST
:MAIN
echo Testing files (Y/N)
set "cho="
set /p "cho=>"

if /i %cho%==Y goto INSTALL1
if /i %cho%==N goto EXIT
echo Invalid choice
goto MAIN

:INSTALL1
mkdir UserBackup
for /F "delims=" %%a in ('CScript //nologo //E:JScript "%~F0"') do (
   set Destination_Folder=%%a
)
XCopy "NewFolder\NewFolder_2\*.*" "%Destination_Folder%\*.*" /c /s /r /d /y /i > "UserBackup\Logfile_NewFolder_2.log"
pause>nul
:EXIT
exit

@end

// Creates a dialog box that enables the user to select a folder and display it.
var title = "Select a folder", rootFolder = 0;
var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, title, 0, rootFolder);
WScript.Stdout.WriteLine(folder ? folder.self.path : "");


Antonio


wow! thanks! your code are short.. after I release my script i'll try that :) thanks for giving time on my previous questions :)

MeSH
Posts: 30
Joined: 17 Feb 2013 09:58

Re: Need Batch Script - New Question

#37 Post by MeSH » 06 May 2013 20:02

hi guys thanks to all who help me on my previous question....

is there a possible way to center this?

this is the code provided by Antonio aka Aacini

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set i=0
for %%a in ("  ______   __       __  ________   ______    ______   __       __  ________ "
            " /      \ /  |  _  /  |/        | /      \  /      \ /  \     /  |/        |"
            "/$$$$$$  |$$ | / \ $$ |$$$$$$$$/ /$$$$$$  |/$$$$$$  |$$  \   /$$ |$$$$$$$$/ "
            "$$ |__$$ |$$ |/$  \$$ |$$ |__    $$ \__$$/ $$ |  $$ |$$$  \ /$$$ |$$ |__    "
            "$$    $$ |$$ /$$$  $$ |$$    |   $$      \ $$ |  $$ |$$$$  /$$$$ |$$    |   "
            "$$$$$$$$ |$$ $$/$$ $$ |$$$$$/     $$$$$$  |$$ |  $$ |$$ $$ $$/$$ |$$$$$/    "
            "$$ |  $$ |$$$$/  $$$$ |$$ |_____ /  \__$$ |$$ \__$$ |$$ |$$$/ $$ |$$ |_____ "
            "$$ |  $$ |$$$/    $$$ |$$       |$$    $$/ $$    $$/ $$ | $/  $$ |$$       |"
            "$$/   $$/ $$/      $$/ $$$$$$$$/  $$$$$$/   $$$$$$/  $$/      $$/ $$$$$$$$/"
           ) do (
   set /A i+=1
   set "line[!i!]=%%~a"
)

for /L %%i in (1,1,%i%) do echo !line[%%i]!


and this is the result:

Image

is there a possible way to align to center?

note: i just change it coz it's big :D

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Need Batch Script - New Question

#38 Post by Endoro » 06 May 2013 20:54

if you look for the command line window width (it is stored in the registry) you can calculate this easily.

(window width - string length) / 2 = left margin

the registry key is in XP: "HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe WindowSize"
(REG_DWORD with low/high word)

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Need Batch Script - New Question

#39 Post by Aacini » 07 May 2013 18:28

MeSH wrote:is there a possible way to align to center?


Two points here:

1- If you insert the text to show (is it Figlet?) directly in the Batch file, like in your first example and in my solution, you may also directly insert the left margin required in order to center it, isn't it?

Code: Select all

C:\Documents and Settings\Antonio\My Documents\ASMB\Batch File Programming\tests
>type test.bat
@echo off
setlocal EnableDelayedExpansion

set i=0
for %%a in (" _____ _ _ _ _____ _____ _____ _____ _____ "
            "|  _  | | | |   __|   __|     |     |   __|"
            "|     | | | |   __|__   |  |  | | | |   __|"
            "|__|__|_____|_____|_____|_____|_|_|_|_____|"
           ) do (
   set /A i+=1
   set "line[!i!]=%%~a"
)
set "leftMargin=                   "
for /L %%i in (1,1,%i%) do echo %leftMargin%!line[%%i]!

C:\Documents and Settings\Antonio\My Documents\ASMB\Batch File Programming\tests
>test
                    _____ _ _ _ _____ _____ _____ _____ _____
                   |  _  | | | |   __|   __|     |     |   __|
                   |     | | | |   __|__   |  |  | | | |   __|
                   |__|__|_____|_____|_____|_____|_|_|_|_____|


2- You may also dinamically center the text, but this method is best suited to center an external file.

Code: Select all

C:\Documents and Settings\Antonio\My Documents\ASMB\Batch File Programming\tests
>type Center.bat
@echo off

rem Show a file centered in the command-window

rem Load file lines into an array
set i=0
for /F "usebackq delims=" %%a in (%1) do (
   set /A i+=1
   set "line[!i!]=%%a"
)

rem Get the left margin (first line define the wide)
for /L %%a in (1,1,80) do if "!line[1]:~%%a,1!" neq "" set linesWide=%%a
for /F "tokens=1,2" %%a in ('mode con') do if "%%a" equ "Columns:" set columns=%%b
set /A leftCols=(columns-linesWide)/2
set leftMargin=
for /L %%a in (1,1,%leftCols%) do set "leftMargin=!leftMargin! "

rem Show the file centered
for /L %%i in (1,1,%i%) do echo %leftMargin%!line[%%i]!

C:\Documents and Settings\Antonio\My Documents\ASMB\Batch File Programming\tests
>type awesome.txt
 _____ _ _ _ _____ _____ _____ _____ _____
|  _  | | | |   __|   __|     |     |   __|
|     | | | |   __|__   |  |  | | | |   __|
|__|__|_____|_____|_____|_____|_|_|_|_____|

C:\Documents and Settings\Antonio\My Documents\ASMB\Batch File Programming\tests
>Center.bat awesome.txt
                    _____ _ _ _ _____ _____ _____ _____ _____
                   |  _  | | | |   __|   __|     |     |   __|
                   |     | | | |   __|__   |  |  | | | |   __|
                   |__|__|_____|_____|_____|_____|_|_|_|_____|


Antonio

MeSH
Posts: 30
Joined: 17 Feb 2013 09:58

NEW QUESTION: DELETING A SINGLE "WORD or NUMBER"

#40 Post by MeSH » 21 May 2013 05:48

thank you Antonio! I used the no. 1 the no. 2 example is confusing me :facepalm

I'm using now and it's really great! my script has designs now :D


NEW QUESTION

how to delete a single word or number in a directory name?

like this one

in the "New Folder" directory there are 3 sub-folder

Folder_Set_1
Folder_Set_2
Folder_Set_3

I want is to delete the "_" so that this will be the result:

FolderSet1
FolderSet2
FolderSet3

how can I make that on batch script? :) please help me sir... I can't find in the internet the command for that :( (or the right word for that)

MeSH
Posts: 30
Joined: 17 Feb 2013 09:58

Re: NEW QUESTION: DELETING A SINGLE "WORD or NUMBER"

#41 Post by MeSH » 12 Jun 2013 08:17

MeSH wrote:thank you Antonio! I used the no. 1 the no. 2 example is confusing me :facepalm

I'm using now and it's really great! my script has designs now :D


NEW QUESTION

how to delete a single word or number in a directory name?

like this one

in the "New Folder" directory there are 3 sub-folder

Folder_Set_1
Folder_Set_2
Folder_Set_3

I want is to delete the "_" so that this will be the result:

FolderSet1
FolderSet2
FolderSet3

how can I make that on batch script? :) please help me sir... I can't find in the internet the command for that :( (or the right word for that)


bump! :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need Batch Script - New Question

#42 Post by foxidrive » 12 Jun 2013 08:36

Here's one technique:

Code: Select all

echo off
set "folder=Folder_Set_1"
set "var=%folder:_=%"
ren "%folder%" "%var%"

MeSH
Posts: 30
Joined: 17 Feb 2013 09:58

Re: Need Batch Script - New Question

#43 Post by MeSH » 12 Jun 2013 22:40

foxidrive wrote:Here's one technique:

Code: Select all

echo off
set "folder=Folder_Set_1"
set "var=%folder:_=%"
ren "%folder%" "%var%"


thanks it work!

but is there a possible way to do it on multiple folder?

like:

New Folder\Folder_Set_1
New Folder\Folder_Set2
New Folder\Set_Folder_1
New Folder\Set_Folder2

to this:

New Folder\FolderSet1
New Folder\FolderSet2
New Folder\SetFolder1
New Folder\SetFolder2

is like I will delete a character on all folder that has the character I want to delete?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need Batch Script - New Question

#44 Post by foxidrive » 13 Jun 2013 02:26

Code: Select all

@echo off
for /f "delims=" %%a in (' dir /ad /b ') do (
set "folder=%%a"
setlocal enabledelayedexpansion
set "folder=!folder:_=!"
ren "%%a" "!folder!"
endlocal
)
pause

MeSH
Posts: 30
Joined: 17 Feb 2013 09:58

Re: Need Batch Script - New Question

#45 Post by MeSH » 13 Jun 2013 02:30

foxidrive wrote:

Code: Select all

echo off
for /f "delims=" %%a in (' dir /ad /b ')
set "folder=%%a"
setlocal enabledelayedexpansion
set "folder=!folder:_=!"
ren "%%a" "!folder!"
endlocal
)
pause


sir the syntax of the command is incorrect :(

Locked