I'm trying to make a simple script that will output the names of all subfolders, except for one. I'm having trouble doing the string comparison to remove that one folder.
Here's a basic folder breakdown:
- C
- Backup
- .sync
- folder1
- folder2
- Backup
Here's a basic example of code that works 90%:
ForFiles /P C:\Backup /C "CMD /C echo @FILE"
And here's the output of this command (the quotation marks are in the output itself):
".sync"
"folder1"
"folder2"
To give a brief explanation of the code, basically ForFiles will run through the files and subfolders at the given path (specified by /P C:\Backup
). /C
then specifies a command, which is in double quotes - echo @FILE
will then output the name of the file. The actual code I'm running is more complex than this, but this will suffice as a minimum example showing my problem. I do not want to output the name of the .sync folder, because it's used by another program and should not be touched.
So, what I want to do is put an "if" statement in the command, so that not everything will be output. In pseudocode, this would be "if the name of the folder is not .sync, output the name of the folder". However, this appears to be more complex because of the quotation marks, which I guess I have to escape - but I have not had any luck with it. Here are some things I've tried:
- Outputs all files, including .sync:
ForFiles /P C:\Backup /C "CMD /C if NOT @FILE == .sync echo @FILE"
ForFiles /P C:\Backup /C "CMD /C if NOT @FILE == ".sync" echo @FILE"
ForFiles /P C:\Backup /C "CMD /C if NOT @FILE == '.sync' echo @FILE"
- Gives an error because of the double double quotes (one method of escaping the double quotes, found on Stack Overflow)
ForFiles /P C:\Backup /C "CMD /C if NOT @FILE == "".sync"" echo @FILE"
- Outputs all files, including .sync - using the ^ symbol was the other method of escaping double quotes I found on Stack Overflow, and it appears only the first one is actually being escaped for some reason
ForFiles /P C:\Backup /C "CMD /C if NOT @FILE == ^".sync^" echo @FILE"
Can anyone give me some advice on howq to acocomplish this? Thanks!