Do you speak Batch?

Any other tech-related topics
Message
Author
spicydog
Posts: 262
Joined: Mon Mar 16, 2009 11:56 am

Do you speak Batch?

#1 Post by spicydog »

Hey there,

Two quick questions for the batch gurus:

1) Can a batch file swap the names of two specific files (or folders) existing at a specific path on the local drive?

2) Can a batch file move to the recycle bin (not delete) a specific file or a folder existing at a specific path on the local drive?

For *both* points above please note that:

a) The batch file should be run from a location which is not the same as the one of the file(s)/folder(s) to be swapped/moved to the recycle bin

b) The path(s) of the file(s)/folder(s) to be swapped/moved to the recycle bin contain(s) some spaces

c) The commands should run silently and without asking for swap (or delete) confirmations

Thanks in advance for the creative feedback :mrgreen:

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: Do you speak Batch?

#2 Post by guinness »

1) Can a batch file swap the names of two specific files (or folders) existing at a specific path on the local drive?
Not possible as this kind of requirement is for a program language not a batch script.
2) Can a batch file move to the recycle bin (not delete) a specific file or a folder existing at a specific path on the local drive?
This isn't possible with a BATCH Script.

Good reference for creating batch scripts is >> http://ss64.com/nt/

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: Do you speak Batch?

#3 Post by guinness »

As Swap'em was requested by you I can add additional parameters if you want then in a batch script you would use SwapEm.exe "C:\Path" "D:\NewPath" /recycle.

Edit:
Right now you can do this in a batch script SwapEm.exe "C:\Path" "D:\NewPath"
Last edited by guinness on Wed Jan 11, 2012 3:04 am, edited 1 time in total.

spicydog
Posts: 262
Joined: Mon Mar 16, 2009 11:56 am

Re: Do you speak Batch?

#4 Post by spicydog »

Hey guinness :D

Swap'em is absolutely great and I use it several times a day.

I need the batch file(s) for the PC of a senior person (not too computer savvy) that I am helping sometimes.

To address his needs I will run a Group Policy script at Startup/Shutdown (or Logon/Logoff) and I thought that in this particular case a batch file (containing the exact/specific instructions to complete these tasks) would be more appropriate, especially since these tasks should be running totally unattended (without the user interactions).

I was under the impression that a batch could take care of (at least) the swapping task... I must have been wrong.

Please feel free to contradict my uneducated guess :wink: and kindly suggest a better alternative solution,

Thanks again :mrgreen:

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: Do you speak Batch?

#5 Post by guinness »

I was under the impression that a batch could take care of (at least) the swapping task... I must have been wrong.
Correct the swapping task is possible, but detecting whether if it's a file or folder is not, this is the work of a programming language.

The alternative is this, create a batch script like below and use Swap'em and a small application I will create for your needs e.g.

Code: Select all

SwapEm.exe "C:\Path" "D:\NewPath"
RecyCom.exe "C:\DeleteThisFile.txt" "C:\DeleteThisFileAsWell.txt"
Last edited by guinness on Wed Jan 11, 2012 3:04 am, edited 1 time in total.

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: Do you speak Batch?

#6 Post by guinness »

Here is RecyCom >> http://softwarespot.wordpress.com/code/recycom/

Any problem just let me know. The application took me about 15mins to write and the rest was writing the documentation.

User avatar
m^(2)
Posts: 890
Joined: Sat Mar 31, 2007 2:38 am
Location: Kce,PL
Contact:

Re: Do you speak Batch?

#7 Post by m^(2) »

Batch is Turing complete, you can do anything in it. But it's rarely the best tool for the job.

User avatar
dany
Posts: 70
Joined: Fri Jun 17, 2011 10:26 pm
Location: netherlands

Re: Do you speak Batch?

#8 Post by dany »

guinness wrote:Correct the swapping task is possible, but detecting whether if it's a file or folder is not, this is the work of a programming language.
Not entirely true, You could use the output of DIR to find out if it's a file or directory. However, that would require some advanced magic and it's indeed better to use a more advanced programming language for that. It's not only easier but also a lot saver in terms of error-checking.
If you're lucky you can access the Recycle Bin through C:\RECYCLER or {Drive}:\$Recycle.Bin where {Drive} is usually C. Whether you can send files there from a batch is another matter...
Check out Rob van der Woude's batch pages, also an excellent resource site for batch scripting.

If you want it all in a single script native to Windows and without additional applications then go for VBS. Testing for file types or accessing the Recycle Bin is easy in VBS. You can even embed VBS code in a batch file!
If below code is saved as test.bat and ran from a CMD it should display ye olde 'Hello World', although I haven't tested it (currently at work). Should work though, if I remembered the trick correctly.

Code: Select all

SET Z=nothing:REM & cscript "%~f0" //E:VBS //NOLOGO & GOTO:EOF
WScript.Echo "Hello World"

User avatar
m^(2)
Posts: 890
Joined: Sat Mar 31, 2007 2:38 am
Location: Kce,PL
Contact:

Re: Do you speak Batch?

#9 Post by m^(2) »

If you're lucky you can access the Recycle Bin through C:\RECYCLER or {Drive}:\$Recycle.Bin where {Drive} is usually C. Whether you can send files there from a batch is another matter...
You can do anything. And if you know the right path (isn't it that the correct way to find it is to use registry?), file operations are not different from what you do elsewhere.
dany wrote:
guinness wrote:Correct the swapping task is possible, but detecting whether if it's a file or folder is not, this is the work of a programming language.
Not entirely true, You could use the output of DIR to find out if it's a file or directory.
If I really had to do it in batch, I would use 'for' to iterate over files. Or maybe I would quit.

User avatar
dany
Posts: 70
Joined: Fri Jun 17, 2011 10:26 pm
Location: netherlands

Re: Do you speak Batch?

#10 Post by dany »

m^(2) wrote:isn't it that the correct way to find it is to use registry?
Yes, absolutely right. I rarely use it, so I forgot about that one.
m^(2) wrote:If I really had to do it in batch, I would use 'for' to iterate over files. Or maybe I would quit.
Yea, but FOR alone works only on files or folders, not both and that's what spicydog is asking (if I misunderstood then DIR is not needed). FOR can be used to determine the type though that's true.
Here's how I'd do it:

Code: Select all

@ECHO OFF
:: Very basic working example. Not nearly as magical as I thought :)
:: http://www.robvanderwoude.com/ntforfileattributes.php
FOR /F "delims=" %%A IN ('DIR /B') DO (
    ECHO.%%~aA | FINDSTR /R /I "^d[\-r]" >NUL
    IF NOT ERRORLEVEL 1 (
        CALL:DIR "%%~fA"
    ) ELSE (
        ECHO.%%~aA | FINDSTR /R /I "^-[\-r][act]*" >NUL
        IF NOT ERRORLEVEL 1 (
            CALL:FILE "%%~fA"
        ) ELSE (
            CALL:OTHER "%%~fA"
        )
    )
)
GOTO:EOF

:DIR
ECHO.Directory %~1
GOTO:EOF

:FILE
ECHO.File %~1
GOTO:EOF

:OTHER
ECHO.Probably best left alone %~1
GOTO:EOF
edit: small correction in code

Hydaral
Posts: 194
Joined: Tue Mar 09, 2010 7:36 pm

Re: Do you speak Batch?

#11 Post by Hydaral »

spicydog wrote:Hey there,

Two quick questions for the batch gurus:

1) Can a batch file swap the names of two specific files (or folders) existing at a specific path on the local drive?
Easy, just pass this the two files or directories as command line arguments. Note that I do not do any existing file checks:

Code: Select all

@echo off

rem Get the full paths of the objects
set objectpath1=%~dpnx1
set objectpath2=%~dpnx2

rem Get the full paths of the parent objects
set parentpath1=%~dp1
set parentpath2=%~dp2

set temp=~%random%

rem Move the first object to a temporary location
mv "%objectpath1%" "%parentpath1%%temp%"

rem Move the second object to the first's name
mv "%objectpath2%" "%objectpath1%"

rem Move the temporary object the the second's name
mv "%parentpath1%%temp%" "%objectpath2%"
spicydog wrote:2) Can a batch file move to the recycle bin (not delete) a specific file or a folder existing at a specific path on the local drive?
Recycle Bin is a Windows Explorer thing, cmd.exe has never heard of it. I believe you asked me about this through email a while ago. Someone wrote a CLI app that moves files to the recycle bin:

https://github.com/kizzx2/cmd-recycle/

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: Do you speak Batch?

#12 Post by guinness »

Recycle Bin is a Windows Explorer thing, cmd.exe has never heard of it. I believe you asked me about this through email a while ago. Someone wrote a CLI app that moves files to the recycle bin:
I did too (see above)

Hydaral
Posts: 194
Joined: Tue Mar 09, 2010 7:36 pm

Re: Do you speak Batch?

#13 Post by Hydaral »

guinness wrote:
Hydaral wrote:Recycle Bin is a Windows Explorer thing, cmd.exe has never heard of it. I believe you asked me about this through email a while ago. Someone wrote a CLI app that moves files to the recycle bin:
I did too (see above)
I did see that, but yours is 305 kB, cmd-recycle is 6.5kB. And I almost always prefer to use apps that are written in lower-level languages.

Hmmm, I just noticed that cmd-recycle is written in C#, while that does beat AutoIt in the language level stakes, I have to balance that with my hatred of .NET. :) I've never had a need to use cmd-recycle yet, maybe I'll try to find one written in C.

Edit: Just found one: http://techie-buzz.com/softwares/recycl ... rompt.html

Seems to be Visual C/C++, but close enough.

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: Do you speak Batch?

#14 Post by guinness »

Oh well, only wasted 30 minutes of yesterday!
Last edited by guinness on Tue Feb 14, 2012 4:04 pm, edited 1 time in total.

spicydog
Posts: 262
Joined: Mon Mar 16, 2009 11:56 am

Re: Do you speak Batch?

#15 Post by spicydog »

Gee guys, I don't know what to say... THANKS to all of you for participating so actively to this thread :mrgreen: I took good note of all your advices and downloaded the suggested files and code snippets.

Very nice of guinness to have provided one of his excellent 'ad hoc' solutions.

In the next days I will find out which approach is the most convenient in order to address the issue I mentioned in my earlier post and will add some feedback here.

Gotta run now.... THANKS AGAIN

Post Reply