Listing the files inside a folder using batch file - shell

I am using a batch file to view the contents (xml files) of a certain folder.
I need to use a batch file for it.
Found this command on internet and it is working perfectly when I type in the command prompt, but when I type it in and save as a batch file, it doesn't give any out put at all. (Basically not running the content)
FOR /R D:\Myfolder %F in (*.*) do rename %~nF.xml %~nf1.xml
There are no restrictions in the folder either.

Shouldn't have worked from the prompt.
In a batch file, you need to change any % for the metavariable (%F in this case) to %%.
Having said that however, (or, from the prompt, %F and %f) are two different animals. It's virtually the only situation where batch is case-sensitive. Your command attempts to rename files using their NAME only (~nF) so if it was to encounter fred.txt it would attempt to rename Fred.xml to -er, 1.xml (I think, maybe %~nf.xml)
Best to say what you're trying to do. We're reasonably slick at crufting up solutions...

Related

Batch file flashing command prompt in endless loop after being executed

Background
I want to use ROBOCOPY to backup folders. To learn this, I created a test source folder, containing other subfolders and dummy files.
F:\RoboCopy\RoboCopy_Files
I am able to ROBOCOPY the source folder from the Command line and PowerShell (with using Windows 10).
ROBOCOPY "RoboCopy_Files" "RoboCopy_Files_Testing" /MIR
It does exactly what I want.
Now I put the command into batch file Robocopy.cmd.
Problem symptoms
However, when I put the same command into Robocopy.cmd file, in the root F:\RoboCopy folder and run it, I get only flashing cmd window with my command repeated on ever increasing number of lines.
How can I put the command into a CMD file (e.g. Robocopy.cmd) for later use/share/schedule? How to prevent command prompt from flashing in endless loop without running the command?
Note: I feel this is more about learning how to put cmd scripts into files, than how to use ROBOCOPY.
Cause: File and Command have the same name
I wanted to use the ROBOCOPY command inside the ROBOCOPY file. This is a mistake.
The file ends up calling itself.
Solution 1: Rename batch file
One solution is to rename the batch file to be different from the command(s) in the file.
Solution 2: Use more explicit command call
Another solution could be to use ROBOCOPY.exe or explicitly specify the full path to the exe like C:...\robocopy.exe. This would prevent the "confusion" in calling the command vs calling the batch file itself.
Solution 1 x 2
The best solution (thx Mofi again) is to combine the 1 x 2 together. Use unique batch file name AND specify the full path to command (exe) inside the batch file.
Useful related commands: To determine the full path to command (exe), see the WHERE command (e.g. Where Robocopy.exe - This should be somewhere in windows folder.), system variables (e.g. SS64), or the command SET.
The full version in my case would be to run for example BackupRobocopyFiles.cmd with line:
%SystemRoot%\System32\robocopy.exe "RoboCopy_Files" "RoboCopy_Files_Testing" /MIR /R:2
Note: This would work only if the cmd file is in the root folder F:\RoboCopy. If I would like to run the cmd from different folder (or task sheduler), I would specify the full path to SOURCE_FOLDER and DESTINATION_FOLDER parameters of ROBOCOPY command.
Contributions
The answer was found based on comments by: "MC ND", "eryksun". "Mofi" did point out, that the original Q was not helpful. Thanks to all.

issues in exe files from bat files

ok...Im a new member here and I can express how jolly I am...back to subject
I made a bat file, lets call it 1.bat and I used iexpress to make it an exe file, lets call it 1.exe.
So, in the batch file I added the command line to add a vbs file ( call it 1.vbs ) which is also included in the exe package (1.bat and 1.vbs are in 1.exe) but it installs 1.bat, so in the command I typed:
copy "1.vbs" "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
If I run the bat file it copies the vbs file to start up, but if I make the 1.exe file in iexpress by including 1.bat and 1.vbs and when I click the 1.exe file, it doesnt work, it doesn't copy the vbs file to startup, it says something about too many parameters.
I'm sorry if my post is too long or my question have been posted before, but I couldn't found any thread solves my issues, if there is, i'd be glad to be enlightened.
Thanks.
Never use "Batch to EXE" converter, they often do not work as expected. Simply use the batch script.
Just a note, this file actually only acts as a wrapper for your script, and the script itself actually gets executed in a temp folder created on execution (and deleted afterwards), so make sure you don't use any relative paths.
source
When running a batch file in IExpress, you need to call it like:
cmd /c 1.bat
If you don’t, variables like %username%, long file names, etc, will likely not work.
I suspect you are using short file names. Put in your bat dir then pause and you'll see it's 1~1.vbs or similar.

Pause command if not succeeded instead of automatically closing

Lets say I want to copy a file or run a program, but the file or the location is not found. How can I let my computer pause the command window when these kind of problems occure? I made a batch file to copy files to another location, but also to run programs.
Any help will be appreciated.
In your batch script, use xcopy to do the actual copying, rather than the copy command -
xcopy, unlike copy, returns error codes depending on the result of the copy, which are documented here in the remarks section.
As Aleksandr mentioned, you can use the error codes as part of your script to pause on error.
Assuming you would like to pause on all errors, you could do script this as below:
xcopy /HECY <source> <destination>
if %ERRORLEVEL% NEQ 0 pause
Obviously you'll need to replace and in the above with the locations you are copying from and to respectively.
The /HECY switches are just an example, but in this case could be used to instruct xcopy to copy hidden files, recurse directories, and automatically overwrite files in the destination if they exist. You can tweak these to your specific needs.
Check the error code and pause if it is non zero

Easy task, windows prompt batch file

I am a Windows 7 User who wants to convert multiple *.eps files to pdf-files. Therefore I installed a program called EPSPDF.rb.
I integrated it into my systempath which makes me use it in the command prompt like this:
C:\TEMP>epspdf somefile.eps
The program converts somefile.eps to somefile.pdf.
What I am trying to achieve now is writing a Windows Batch File which will look in
the directory C:\TEMP for all *.eps files and convert them all.
I am still trying hard since I am quite unfamiliar with the programming language.
I guess writing the few lines will be an easy issue for someone who is familiar with Batch Files in Windows. I will be very grateful for any help!
for %%f in (*.eps) do epspfd %%f
This is for use in a batch file. You can run it right on the command prompt, only you need to use %f instead of %%f. Just because. ;)
for %F in (*.eps) do epspdf %F

How to delete a folder that name ended with a dot (".")?

I got some folders created by malware whose name ended with a dot like C:\a.\ or C:\b.\, etc.
I found a solution that can remove such folder with command rd /q /s "C:\a.\" but if I call win API RemoveDirectory, it returns ERROR_FILE_NOT_FOUND.
And I just wonder how to write a function to delete such directory, thanks
I test on my own Windows XP SP3 system like this
create a folder C:\>mkdir a..\\\ and I cannot double click to access this folder. and I can remove with command rd /q /s "C:\a.\"
what Windows system API(s) that rd /q /s command call?
Here's a solution to this problem:
rd /s "\\?\C:\Documents and Settings\User\Desktop\Annoying Folder."
Solution:
When you call RemoveDirectory, make sure that you prefix the path with the string "\\?\".
Explanation:
It has everything to do with the dot. According to MSDN, there are certain cases where you may not be able to delete a file or folder on an NTFS volume, specifically when the file name is invalid in the Win32 name space (which is why you are unable to open the file using the normal methods in Windows Explorer).
You may not be able to delete a file if the file name includes an invalid name (for example, the file name has a trailing space or a trailing period or the file name is made up of a space only). To resolve this issue, use a tool that uses the appropriate internal syntax to delete the file. You can use the "\\?\" syntax with some tools to operate on these files, for example:
del "\\?\c:\path_to_file_that contains a trailing space.txt "
The cause of this issue is similar to Cause 4. However, if you use typical Win32 syntax to open a file that has trailing spaces or trailing periods in its name, the trailing spaces or periods are stripped before the actual file is opened. Therefore, if you have two files in the same folder named "AFile.txt" and "AFile.txt " (note the space after the file name), if you try to open the second file by using standard Win32 calls, you open the first file instead. Similarly, if you have a file whose name is just " " (a space character) and you try to open it by using standard Win32 calls, you open the file's parent folder instead. In this situation, if you try to change security settings on these files, you either may not be able to do this or you may unexpectedly change the settings on different files. If this behavior occurs, you may think that you have permission to a file that actually has a restrictive ACL.
(Source: http://support.microsoft.com/?kbid=320081)
Ive posted this on SU and I decided to post it here too. Its the simplest and fastest and easiest way to achieve this. I am now laughing at how much simple it is.
Install WinRAR
Follow the Step by Step procedure from pictures:
I myself had WinRaR installed so I decided to demonstrate the workaround in it.
This workaround is also possible by using 7zip.
One another thing I should mention is that, as it seems the problem is caused by using windows explorer and any other file browser (like winrar file browser itself, ftp explorers etc.) will treat this files as normal.
You could try using any file browser and simply delete those files and not bother archiving them though!
Cheers!
If you have git installed (you can get ir from here) then it is as simple as:
Navigate File Explorer to location where problematic folder is located.
Context menu (right mouse button) > Git Bash Here.
rm -rf Foldername./
When you see the name is "a.", but the actual name is "a.."
Try this:
rd /q /s "C:\a..\"
And you can try explore the folder by this code:
for /f "tokens=3 delims=<>" %%a in ('dir /ad /x "C:\*" ^| findstr " a\.\.$"') do (
for /f "tokens=1" %%b in ("%%a") do start "" "%%~fb"
)
I used "WinRar" A simple RAR, ZIP processor. You can use any sort of file name editor. Just open the directory where your file is into WinRar and select rename after right clicking the file/folder you want to rename and fill in the new name.
If you need to keep the data you can also use the \\?\ trick for renaming the folder.
ren "\\?\C:\Documents and Settings\User\Desktop\Annoying Folder." "\\?\C:\Documents and Settings\User\Desktop\Annoying Folder"
This is an ideal solution if you need to know what is inside the folder or if the data is important.
This works in both Command Prompt and PowerShell.
Try to use unlocker program to delete files and folders that you can't delete normally.
if you want to keep the files theres options in bash as well.
you will require the Windows Subsystem for Linux package (i have Ubuntu installed)
to keep the files. open a command prompt and cd over to where the file or folder is located.
now type "bash"
this will open bash in the prompt. now enter mv '[folder or file you want to move]' '[new name (can include path)]' (theres more to mv so if you want to read up on all of its options use 'man mv' this will open its manual page (then use q to return to bash))
the mv command is short for move, but its has a secondary function of renaming things.
also in bash use 'single quotes' and not a normal "double quote", as bash expects 'single quotes'.
heres a example. assume your folder is named "data 1." located in c:\users (so the full path to the error folder is c:\users\data 1.
1. open command prompt using any method
2. enter cd c:\users
3. now type bash this loads bash in the folder you previously were in
4. finally type mv 'data 1.' 'data 1'
5. the folder is now accessible and you can choose to delete it.
Use bash rm command from Ubuntu on Windows 10

Resources