|
-
Sep 7th, 2012, 08:33 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Escape Double Quotes Around Path in Shell Command
I have run into a real mind bender here. How does one escape double quotes in a shell command to use a pathname which has a space in it.
Code:
Sub Main()
Shell("cmd /c forfiles /P "S:\Customer Approval" /M *.dwg /D -7 > C:\TEMP\OldFiles.txt")
End Sub
I'm wanting to make a list of the files in our Customer Approval folder which are older than 7 days. The command line syntax of this works fine in a cmd window, but I can't seem to figure out how to escape the double quotes around "S:\Customer Approval". I've tried " "S:\Customer Approval" "...etc but it doesn't seem to work right.
Last edited by Vladamir; Sep 7th, 2012 at 09:14 AM.
-
Sep 7th, 2012, 09:13 AM
#2
Thread Starter
Hyperactive Member
Re: Escape Double Quotes Around Path in Shell Command
Ok, I had to get my head in the game...and here's at least one solution for this dilema
Code:
Sub Main()
Dim myCommand As String = "cmd /c forfiles /P " & Chr(34) & "S:\Customer Approval" & Chr(34) & " /M *.dwg /D -7 > C:\TEMP\Files2Move.txt"
Shell(myCommand)
End Sub
-
Sep 7th, 2012, 02:24 PM
#3
Re: Escape Double Quotes Around Path in Shell Command
If you want to do this with code rather than shelling out to the command processor try the following where you can tweak the columns to any order you like.
VS2010 or higher VB.NET code (if VS2008 add line continuations for broken lines)
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Path As String = "C:\Data"
Dim LogFile As String = "C:\Data\KSG_Results.txt"
If IO.Directory.Exists(Path) Then
Dim Lines =
(
From T In My.Computer.FileSystem.GetFiles(Path, FileIO.SearchOption.SearchTopLevelOnly)
Where CDate(My.Computer.FileSystem.GetFileInfo(T).LastWriteTime) > Now.AddDays(-7) AndAlso
IO.Path.GetExtension(T) = ".txt"
Select String.Concat(
IO.File.GetLastAccessTime(T), ",",
IO.Path.GetFileName(T), ",",
TranslateFileSize(New IO.FileInfo(T).Length))
).ToArray
IO.File.WriteAllLines(LogFile, Lines)
Process.Start(LogFile)
End If
End Sub
Private Function TranslateFileSize(ByVal sender As Long) As String
Dim size As Double = sender
Try
Dim filesizename() As String = {" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"}
Dim pow As Double = Math.Floor(Math.Log(size, 1024))
Return String.Concat(Math.Round(size / Math.Pow(1024, pow), 2), " ", filesizename(CInt(pow)))
Catch ex As Exception
Return CStr(size)
End Try
End Function
End Class
Results (of course you could filter out the log file or create the log file in another folder)
Well I had to create a file and modify one to be in the date range.
Code:
9/7/2012 12:06:26 PM,KSG_Results.txt,95 Bytes
9/7/2012 12:05:02 PM,New Text Document.txt,0
9/7/2012 12:25:20 PM,Various.txt,435.42 KB
Last edited by kareninstructor; Sep 7th, 2012 at 02:25 PM.
Reason: Indicate this is VS2010 not VS2008
-
Sep 8th, 2012, 10:29 AM
#4
Re: Escape Double Quotes Around Path in Shell Command
Do you have to use 'Shell' anyway ?
The Process Class replaces 'Shell' in .Net:
e.g. Shell("C:\Xxx\Yyy\Prog.exe") now becomes: Process.Start("C:\Xxx\Yyy\Prog.exe")
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
Sep 10th, 2012, 09:28 AM
#5
Thread Starter
Hyperactive Member
Re: Escape Double Quotes Around Path in Shell Command
kevininstructor,
I like this code you've provided. Lot's of new commands for me to investigate. I tried this, set the paths and the extension mask to my setup but it only opened up notepad with an empty file. Can you tell me what I might be doing wrong?
-
Sep 10th, 2012, 11:45 AM
#6
Hyperactive Member
Re: Escape Double Quotes Around Path in Shell Command
If I'm understanding you correctly, you want to use a quote in your string?
Use double quotes inside of your outer quotes.
Code:
Dim str As String = String.Empty
str = """This is in quotes."""
MessageBox.Show(str)
This results in "This is in quotes."
-
Sep 10th, 2012, 01:06 PM
#7
Thread Starter
Hyperactive Member
Re: Escape Double Quotes Around Path in Shell Command
OK, I'm trying to use the code from kevininstructor. The process I'm using is to be run in the background so I made it into a console application.
Code:
Module Module1
Sub Main()
Dim Path As String = "H:\"
Dim LogFile As String = "C:\0\LogFile.txt"
If IO.Directory.Exists(Path) Then
Dim Lines =
(
From T In My.Computer.FileSystem.GetFiles(Path, FileIO.SearchOption.SearchTopLevelOnly)
Where CDate(My.Computer.FileSystem.GetFileInfo(T).LastWriteTime) <= Now.AddDays(-7) AndAlso
IO.Path.GetExtension(T) = ".txt"
Select String.Concat(
IO.File.GetLastAccessTime(T), ",",
IO.Path.GetFileName(T))
).ToArray
IO.File.WriteAllLines(LogFile, Lines)
Process.Start(LogFile)
End If
End Sub
End Module
This works pretty good except it seems to list all the files. Not just the one's which have a LastWriteTime of seven days ago or more. These are all new commands for me so I'm needing some help understanding what's going wrong.
-
Sep 10th, 2012, 01:42 PM
#8
Re: Escape Double Quotes Around Path in Shell Command
 Originally Posted by Vladamir
kevininstructor,
I like this code you've provided. Lot's of new commands for me to investigate. I tried this, set the paths and the extension mask to my setup but it only opened up notepad with an empty file. Can you tell me what I might be doing wrong?
I am on a different computer today which also has the path C:\Data but with no text files in the past seven day range which means the first time using the code I posted I got nothing as it should be. Ran it again and got KSG_Results.txt which is correct code wise but not for you. So I added to the where clause so that the log file KSG_Results.txt is excluded, ran the code again and got zero files which is correct. Next I created several new text files in C:\Data, ran the code and got only the new files back.
With the testing above and getting back exactly what I suspected means the code works as I only tested it for text files (.txt) where each file is done with
Code:
IO.Path.GetExtension(T) = ".txt"
which excludes files that have upper cased extensions i.e. MyFile.TXT is not reported while MyFile.txt is reported. So we need to alter the code as follows
Code:
IO.Path.GetExtension(T).ToLower = ".txt"
Test it as follows, comment out the last two lines and simple display results to the IDE Output window
Code:
Public Sub DemoXzy()
Dim Path As String = "C:\Data"
Dim LogFile As String = "C:\Data\KSG_Results.txt"
If IO.Directory.Exists(Path) Then
Dim Lines =
(
From T In My.Computer.FileSystem.GetFiles(Path, FileIO.SearchOption.SearchTopLevelOnly)
Where CDate(My.Computer.FileSystem.GetFileInfo(T).LastWriteTime) > Now.AddDays(-7) AndAlso
IO.Path.GetExtension(T).ToLower = ".txt" AndAlso Not (IO.Path.GetFileName(T) = IO.Path.GetFileName(LogFile))
Select String.Concat(
IO.File.GetLastAccessTime(T), ",",
IO.Path.GetFileName(T), ",",
TranslateFileSize(New IO.FileInfo(T).Length))
).ToArray
Console.WriteLine(Lines.Count)
For Each item In Lines
Console.WriteLine("[{0}]", item)
Next
'IO.File.WriteAllLines(LogFile, Lines)
'Process.Start(LogFile)
End If
End Sub
Always visually check what is in the folder you are checking, count how many files should be returned.
Any ways let's go one step more by making things more variable.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DemoXzy("C:\Data", "C:\Data\KSG_Results.txt", "txt", True)
End Sub
Code behind
Code:
''' <summary>
'''
''' </summary>
''' <param name="Path">Folder to get files from</param>
''' <param name="LogFile">Folder and file name to write results too</param>
''' <param name="WriteLog">True writes log while False displays results to IDE output window</param>
''' <remarks></remarks>
Public Sub DemoXzy(ByVal Path As String, ByVal LogFile As String, ByVal Extension As String, ByVal WriteLog As Boolean)
If Extension(0) <> "." Then
Extension = "." & Extension
End If
Extension = Extension.ToLower
If IO.Directory.Exists(Path) Then
Dim Lines =
(
From T In My.Computer.FileSystem.GetFiles(Path, FileIO.SearchOption.SearchTopLevelOnly)
Where CDate(My.Computer.FileSystem.GetFileInfo(T).LastWriteTime) > Now.AddDays(-7) AndAlso
IO.Path.GetExtension(T).ToLower = Extension AndAlso
Not (IO.Path.GetFileName(T) = IO.Path.GetFileName(LogFile))
Select String.Concat(
IO.File.GetLastAccessTime(T), ",",
IO.Path.GetFileName(T), ",",
TranslateFileSize(New IO.FileInfo(T).Length))
).ToArray
If WriteLog Then
IO.File.WriteAllLines(LogFile, Lines)
Process.Start(LogFile)
Else
Console.WriteLine(Lines.Count)
For Each item In Lines
Console.WriteLine("[{0}]", item)
Next
End If
End If
End Sub
Private Function TranslateFileSize(ByVal sender As Long) As String
Dim size As Double = sender
Try
Dim filesizename() As String = {" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"}
Dim pow As Double = Math.Floor(Math.Log(size, 1024))
Return String.Concat(Math.Round(size / Math.Pow(1024, pow), 2), " ", filesizename(CInt(pow)))
Catch ex As Exception
Return CStr(size)
End Try
End Function
-
Sep 10th, 2012, 02:26 PM
#9
Thread Starter
Hyperactive Member
Re: Escape Double Quotes Around Path in Shell Command
Kevin,
Thanks again for your reply. I took your advice and made a quick check of what *.txt files are in my H:\ folder. It came up like this:
Directory of H:\
08/29/2012 04:12 PM 1,105 .AcadOpenDwg.txt
01/20/2012 09:32 AM 4 ACADVersion.txt
09/07/2012 03:33 PM 316 APD.txt
08/24/2012 11:50 AM 325 APDR.txt
09/07/2012 03:38 PM 316 APDSS.txt
06/06/2012 09:49 AM 333 APDSSR.txt
09/10/2012 10:01 AM 275 APS.txt
08/24/2012 02:02 PM 283 APSR.txt
07/20/2012 03:58 PM 25,206 AutoCount.txt
07/23/2012 03:46 PM 248,913 email.txt
05/29/2012 02:45 PM 980 newtextfile.txt
07/24/2012 10:38 AM 0 Testing123.txt
08/27/2012 09:48 AM 289 TPD.txt
06/19/2012 11:09 AM 274 TPD1.txt
08/07/2012 04:41 PM 413 TPDSS.txt
08/27/2012 12:04 PM 292 TPS.txt
08/31/2012 11:00 AM 4,556 VBA-Setpage.txt
08/14/2012 01:59 PM 17 vDwgName.txt
18 File(s) 283,897 bytes
0 Dir(s) 198,684,688,384 bytes free
A total of 18 files and you can see the dates associated with them in this listing. So, I run the above code (my previous one, not your most recent one) and I get a list back like this:
8/30/2012 9:49:26 AM,.AcadOpenDwg.txt
8/27/2012 12:04:42 PM,ACADVersion.txt
8/24/2012 11:53:26 AM,APDR.txt
7/24/2012 11:53:26 AM,APDSSR.txt
8/24/2012 2:02:08 PM,APSR.txt
8/6/2012 8:48:28 AM,AutoCount.txt
7/23/2012 3:46:00 PM,email.txt
5/29/2012 2:46:10 PM,newtextfile.txt
7/24/2012 10:38:14 AM,Testing123.txt
9/10/2012 2:49:44 PM,TPD.txt
7/24/2012 11:53:26 AM,TPD1.txt
8/7/2012 4:41:16 PM,TPDSS.txt
8/27/2012 12:04:44 PM,TPS.txt
8/31/2012 11:00:00 AM,VBA-Setpage.txt
8/14/2012 1:59:34 PM,vDwgName.txt
I'm not that familiar with LastWriteTime vs the actual date shown when I do a directory. I'm also a little confused as to why some of these files are showing a LastWriteTime as late as they are because AFAIK they shouldn't have been changed...however, let's assume everything is ok. I want to get a listing of all the files with the last time they were saved was 7 or more days from the current date. I'm confused as to why the file TPD.txt has a date when I do a typical directory from the command line of 8/27/2012 but then the LastWriteTime date is 9/10/2012. If the file was written to on 9/10/2012, how can the directory be showing it as 8/27/2012? I need some straightening out on this principal because I'm a little confused.
-
Sep 10th, 2012, 02:40 PM
#10
Re: Escape Double Quotes Around Path in Shell Command
If I change from my C drive (C:\Data) to one of my drives on a Windows server GetLastWriteTime and GetLastAccessTime are correctly reported but on my C drive both are reported to be the same even thou they are not. So it appears that at least in this test results are different from local drive to network drive.
To see this try the changed code below (it is slightly different but does the same thing)
Note this code is a tad more complex if you have never used xml literals (see FormatLine variable) used to format a string via String.Format.
Also note in all these examples I could compact several lines of code into one but that makes it even harder to learn from.
Code:
''' <summary>
'''
''' </summary>
''' <param name="Path">Folder to get files from</param>
''' <param name="LogFile">Folder and file name to write results too</param>
''' <param name="WriteLog">True writes log while False displays results to IDE output window</param>
''' <remarks></remarks>
Public Sub DemoXzy(ByVal Path As String, ByVal LogFile As String, ByVal Extension As String, ByVal WriteLog As Boolean)
If Extension(0) <> "." Then
Extension = "." & Extension
End If
Extension = Extension.ToLower
If IO.Directory.Exists(Path) Then
Dim Lines =
(
From T In My.Computer.FileSystem.GetFiles(Path, FileIO.SearchOption.SearchTopLevelOnly)
Where CDate(My.Computer.FileSystem.GetFileInfo(T).LastWriteTime) >= Now.AddDays(-7) AndAlso
IO.Path.GetExtension(T).ToLower = Extension AndAlso
Not (IO.Path.GetFileName(T) = IO.Path.GetFileName(LogFile))
Select New With
{
.LastWriteTime = IO.File.GetLastWriteTime(T),
.LastAccessTime = IO.File.GetLastAccessTime(T),
.Name = IO.Path.GetFileName(T),
.Size = TranslateFileSize(New IO.FileInfo(T).Length)
}
).ToArray
Dim Results = (From T In Lines Order By T.Name).ToArray
Dim LongestNameSize = (From T In Lines Select T.Name.Length).Max
'
' Format line so things are clear for debugging only
'
Dim FormatLine As String = <T>{0,-<%= LongestNameSize + 2 %>},{1},{2},{3}</T>.Value
If WriteLog Then
Dim sb As New System.Text.StringBuilder
For Each item In Lines
sb.AppendLine(String.Format(FormatLine, item.Name, item.LastWriteTime, item.LastAccessTime, item.Size))
Next
IO.File.WriteAllText(LogFile, sb.ToString)
Process.Start(LogFile)
Else
Console.WriteLine(Lines.Count)
For Each item In Lines
Console.WriteLine(FormatLine, item.Name, item.LastWriteTime, item.LastAccessTime, item.Size)
Next
End If
End If
End Sub
-
Sep 10th, 2012, 02:56 PM
#11
Thread Starter
Hyperactive Member
Re: Escape Double Quotes Around Path in Shell Command
I've been meaning to find out more about those xml lines. I assume by that you are referring to the <summary> # REGION, etc... I've seen some programs with this format and again I need to read more on the subject matter. It's a skill I need to master to get the programs I develop in a form which can be used by others later in this life cycle.
-
Sep 10th, 2012, 03:00 PM
#12
Re: Escape Double Quotes Around Path in Shell Command
 Originally Posted by Vladamir
I've been meaning to find out more about those xml lines. I assume by that you are referring to the <summary> # REGION, etc... I've seen some programs with this format and again I need to read more on the subject matter. It's a skill I need to master to get the programs I develop in a form which can be used by others later in this life cycle.
Let's say you have
Code:
Private Function SomeRoutines(ByVal LastName As String) As Boolean
Return True
End Function
Directly above the function type ''' and you get
Code:
''' <summary>
'''
''' </summary>
''' <param name="LastName"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function SomeRoutines(ByVal LastName As String) As Boolean
Return True
End Function
Fill in something in param LastName and when you use the function it shows what you place there while coding which can be helpful to assist with using the function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|