Do you speak Batch?
Do you speak Batch?
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
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
Re: Do you speak Batch?
Not possible as this kind of requirement is for a program language not a batch script.1) Can a batch file swap the names of two specific files (or folders) existing at a specific path on the local drive?
This isn't possible with 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?
Good reference for creating batch scripts is >> http://ss64.com/nt/
Re: Do you speak Batch?
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"
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.
Re: Do you speak Batch?
Hey guinness
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 and kindly suggest a better alternative solution,
Thanks again
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 and kindly suggest a better alternative solution,
Thanks again
Re: Do you speak Batch?
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.I was under the impression that a batch could take care of (at least) the swapping task... I must have been wrong.
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.
Re: Do you speak Batch?
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.
Any problem just let me know. The application took me about 15mins to write and the rest was writing the documentation.
Re: Do you speak Batch?
Batch is Turing complete, you can do anything in it. But it's rarely the best tool for the job.
Re: Do you speak Batch?
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.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.
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"
Re: Do you speak Batch?
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.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...
If I really had to do it in batch, I would use 'for' to iterate over files. Or maybe I would quit.dany wrote:Not entirely true, You could use the output of DIR to find out if it's a file or directory.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.
Re: Do you speak Batch?
Yes, absolutely right. I rarely use it, so I forgot about that one.m^(2) wrote:isn't it that the correct way to find it is to use registry?
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.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.
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
Re: Do you speak Batch?
Easy, just pass this the two files or directories as command line arguments. Note that I do not do any existing file checks: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?
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%"
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: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?
https://github.com/kizzx2/cmd-recycle/
Re: Do you speak Batch?
I did too (see above)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:
Re: Do you speak Batch?
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.guinness wrote:I did too (see above)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:
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.
Re: Do you speak Batch?
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.
Re: Do you speak Batch?
Gee guys, I don't know what to say... THANKS to all of you for participating so actively to this thread 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
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